我正在尝试找到一组的所有可能组合,但元素的顺序对我的问题也很重要。
例如,对于集合set={A, B, C}
,可能的子集为subsets={A},{B},{C},{A,B},{A,C},{B,A},{B,C},{C,A},{C,B},{A,B,C},{A,C,B},{B,A,C},{B,C,A},{C,A,B},{C,B,A}
。
是否有任何Matlab函数可以找到它?
提前致谢。
答案 0 :(得分:3)
我无法忍受在Tommaso's answer中看到所有for
个循环,所以这里没有循环:
a = {'A' 'B' 'C'};
% Build an array of binary vectors which we will use to select subsets of
% the array to be permuted
nGroups = 2^numel(a) - 1;
selector = dec2bin(1:nGroups) == '1'; % hack to convert numbers to binary vectors
selectVectors = arrayfun(@(x) selector(x,:), 1:size(selector, 1), 'UniformOutput', false);
% Get the permutations of each subset of the array
permsCell = cellfun(@(s) perms(a(s)), selectVectors, 'UniformOutput', false);
% Rearrange the permutations into a one-dimensional cell array with one
% permutation in each element
rowsAsCells = @(ca) arrayfun(@(x) ca(x,:), 1:size(ca,1), 'UniformOutput', false);
permsAsRows = cellfun(rowsAsCells, permsCell, 'UniformOutput', false);
result = cat(2, permsAsRows{:});
这会返回一个单元格数组,每个元素都有一个排列,就像Tommaso的第二个解决方案一样。
这个版本是否更可取是一个品味问题; - )
答案 1 :(得分:1)
a = {'A' 'B' 'C'};
a_len = numel(a);
res_len = 0;
for i = 1:a_len
res_len = res_len + (factorial(a_len) / factorial(a_len - i));
end
res = cell(res_len,a_len);
res_off = 1;
for i = 1:a_len
bin = nchoosek(a,i);
for j = 1:size(bin,1)
bin_j = bin(j,:);
per = perms(bin_j);
per_hei = size(per,1);
res_ran = res_off + per_hei - 1;
res(res_off:res_ran,:) = [per repmat({''},size(per,1),a_len - i)];
res_off = res_off + per_hei;
end
end
或者,如果您不希望将结果组织成相同大小的列,而是将其插入到具有可变长度的单个向量中:
a = {'A' 'B' 'C'};
a_len = numel(a);
res_len = 0;
for i = 1:a_len
res_len = res_len + (factorial(a_len) / factorial(a_len - i));
end
res = cell(res_len,1);
res_off = 1;
for i = 1:numel(a)
bin = nchoosek(a,i);
for j = 1:size(bin,1)
bin_j = bin(j,:);
per = perms(bin_j);
for y = 1:size(per,1)
res{res_off,1} = {per{y,:}};
res_off = res_off + 1;
end
end
end