function main()
combv({[1,2,3], [3,4,5]})
function currset = combv( superset )
% COMBV returns all combinations of a given set of vectors. 'superset' is
% a cell array of vectors to be combined. The output is an array of
% combined vectors. combv is a recursive function. [adam greenberg]
% Note that the output array returned by combv lists combinations of vector
% elements in row-major order, whereas the MATLAB built-in combvec function
% returns combinations in column-major order. To make combv output match
% combvec, call combv as combv( superset )' .
% combv works by recursing down the cell array of vectors. once it gets
% to the bottom, it starts building up the output array by combining
% the last vector of the input array with the penultimate vector. at
% its core, combv is performing a simple map-reduce.
numlevel = length( superset );
numcurr = length( superset{1} );
% if there are any vectors left in this copy of the superset, call
% combv on those vectors.
subset = [];
if numlevel>1
subset = combv( superset(2:end) ); % recurse
end
numsub = size(subset,1);
dimsub = max(1,numsub);
% combine the current level's vector elements with all the vector
% combinations that were determined by previous recursive calls of
% combv. if there was no previous recursive calls (i.e., this level is
% the lowest) then the internal for loop is skipped.
currset = zeros( [ dimsub*numcurr numlevel ] );
for i=1:numcurr
icurr = (i-1)*dimsub + 1;
currset( icurr:icurr+dimsub-1, 1 ) = superset{1}(i);
for j=1:numsub
isub = j-1;
currset( icurr + isub, 2:end ) = subset(j,:);
end
end
end
end
To embed this project on your website, copy the following code and paste it into your website's HTML: