假设我的向量不包含双倍。它们含有细胞。combvec拒绝接受单元值..。例如:
m = {
[cell1, cell2, cell3];
[cell4, cell5];
[cell6];
};我想以某种方式得到一个细胞向量,包含所有可能的细胞组合:[[cell1, cell4, cell6]; [cell1, cell5, cell6]; [cell2, cell4, cell6]; [cell2, cell5, cell6]; [cell3, cell4, cell6]; [cell3, cell5, cell6];];。
怎么做呢?
我之所以这么做是因为我已经分组了项目,我想找到它们的所有组合,所以我想把它们插入到nx1单元中。如果有更好的解决办法,请建议..。
发布于 2018-04-27 08:07:25
只需在表示列索引的整数数组中使用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]}https://stackoverflow.com/questions/50053350
复制相似问题