function orderedSetMatrix = concatenateOrderetSetMatrices(n, kMax, kMin)
% obtain combinatorics for selecting up to k elements from n
%
% not-selected elements are indicated by 0
%
% THIS IS NO USER FUNCTION

% The elk-library: convex geometry applied to crystallization modeling.
%   Copyright (C) 2014 Alexander Reinhold
%
% This program is free software: you can redistribute it and/or modify it 
%   under the terms of the GNU General Public License as published by the 
%   Free Software Foundation, either version 3 of the License, or (at your 
%   option) any later version.
%
% This program is distributed in the hope that it will be useful, but 
%   WITHOUT ANY WARRANTY; without even the implied warranty of 
%   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
%   General Public License for more details.
%
% You should have received a copy of the GNU General Public License along 
%   with this program.  If not, see <http://www.gnu.org/licenses/>.

%% input handling
if ~exist('kMin', 'var') || isempty(kMin)
    kMin = 0;
end

%% determine output size
amountVector = nan(1, (n+1) - kMin - (n-kMax));
for iAmount = kMin:kMax
    amountVector(iAmount - kMin + 1) = nchoosek(n, iAmount);
end
startIndexVector = [0 cumsum(amountVector)];
orderedSetMatrix = zeros(startIndexVector(end), kMax);


%% fill matrix

% cycle through the total number of elements to be selected
for iAmount = kMin:kMax
    if iAmount == 0
        thisOrderedSetMatrix = [];
    else
        thisOrderedSetMatrix = obtainOrderedSetMatrix(n, iAmount);
    end
    orderedSetMatrix(...
        (startIndexVector(iAmount - kMin + 1)+1): ...
         startIndexVector(iAmount - kMin + 2), 1:iAmount) = ... 
          thisOrderedSetMatrix(end:-1:1, :);
end