仅使用
combvec
使用表示列索引的整数数组,然后使用该数组索引原始数组
C = {[{1} {2} {3}]; [{4} {5}]; [{6}]}
cv = combvec(1:3, 1:2, 1)
out = [C{1}(1,cv(1,:)); C{2}(1,cv(2,:)); C{3}(1,cv(3,:))];
你可以这样概括(可能有更简洁的方法)
idx = cellfun(@(x) 1:numel(x), C, 'uni', 0); % set up indexing array
cv = combvec(idx{:}); % get combinations
out = arrayfun(@(x) C{x}(1,cv(x,:)), 1:3, 'uni', 0); % index into the cell array
out = vertcat(out{:}); % concatenate results
% Result
>> out =
{[1]} {[2]} {[3]} {[1]} {[2]} {[3]}
{[4]} {[4]} {[4]} {[5]} {[5]} {[5]}
{[6]} {[6]} {[6]} {[6]} {[6]} {[6]}