将各种大小的元胞数组的元胞数组整理成大小为 {1xN} 的元胞数组的元胞数组
Wrangle cell array of various-sized cell arrays into cell array of cell arrays of size {1xN}
我正在尝试处理 combnk 以生成单元格中的所有字符串组合。例如:
someStrings = {'a','b','dog','goat'};
results = arrayfun(@(k) combnk(someStrings,k),1:length(someStrings),'UniformOutput',0);
这为我提供了一个 4*1
元胞数组,其维度如下:
{4x1 cell} {6x2 cell} {4x3 cell} {1x4 cell}
我想要的是 15*1
个元胞数组,每个元胞数组的大小为 {1xN}
;即 4 个大小为 {1,1}
的元胞数组、6 个大小为 {1,2}
等。我怎样才能有效地做到这一点?
编辑:好的,现在我来了:
results = transpose(arrayfun(@(k) num2cell(combnk(someStrings,k),2),1:length(someStrings),'UniformOutput',0));
finalResults = vertcat(results{:});
是否可以把它变成一行?如何索引元胞数组,如“{:}”,但在创建元胞数组的行中?
您可以通过以下方式实现:
intermResults=cellfun(@(x) num2cell(x,2),results,'uni',0);
finalResults=vertcat(intermResults{:});
说明:如果您查看 results
变量,就会发现这 15 个单元格。您只需要提取出每一行并将其作为一个单元格。这就是 num2cell(x,2)
所做的。通过将它包装成 cellfun
,我将它应用到元胞数组 results
中的每个元胞。现在您有一个 1x4
单元格,其中每行结果都已转换为一个单独的单元格。您只需要将其连接起来即可产生最终答案。这就是 vertcat(intermResults{:})
所做的。
你可以这样做:
m = dec2bin(1:2^numel(someStrings)-1)-'0'; %// each row contains indices of a combination
[~, s] = sort(sum(m,2)); %// compute sum of each row, sort and get sorting indices
m = m(s,:); %// sort rows according to sum
[jj, ii] = find(m.'); %'// find column indices (jj), ordered by row (ii)
result = accumarray(ii, jj, [], @(x){someStrings(x)}); %// group col indices of each row
对于您的示例,这给出了 someStrings
、
>> result
result =
{1x1 cell}
{1x1 cell}
{1x1 cell}
{1x1 cell}
{1x2 cell}
{1x2 cell}
{1x2 cell}
{1x2 cell}
{1x2 cell}
{1x2 cell}
{1x3 cell}
{1x3 cell}
{1x3 cell}
{1x3 cell}
{1x4 cell}
哪里
>> result{1}
ans =
'goat'
>> result{2}
ans =
'dog'
>> result{3}
ans =
'b'
>> result{4}
ans =
'a'
>> result{5}
ans =
'dog' 'goat'
>> result{6}
ans =
'b' 'goat'
[...]
>> result{15}
ans =
'a' 'b' 'dog' 'goat'
我正在尝试处理 combnk 以生成单元格中的所有字符串组合。例如:
someStrings = {'a','b','dog','goat'};
results = arrayfun(@(k) combnk(someStrings,k),1:length(someStrings),'UniformOutput',0);
这为我提供了一个 4*1
元胞数组,其维度如下:
{4x1 cell} {6x2 cell} {4x3 cell} {1x4 cell}
我想要的是 15*1
个元胞数组,每个元胞数组的大小为 {1xN}
;即 4 个大小为 {1,1}
的元胞数组、6 个大小为 {1,2}
等。我怎样才能有效地做到这一点?
编辑:好的,现在我来了:
results = transpose(arrayfun(@(k) num2cell(combnk(someStrings,k),2),1:length(someStrings),'UniformOutput',0));
finalResults = vertcat(results{:});
是否可以把它变成一行?如何索引元胞数组,如“{:}”,但在创建元胞数组的行中?
您可以通过以下方式实现:
intermResults=cellfun(@(x) num2cell(x,2),results,'uni',0);
finalResults=vertcat(intermResults{:});
说明:如果您查看 results
变量,就会发现这 15 个单元格。您只需要提取出每一行并将其作为一个单元格。这就是 num2cell(x,2)
所做的。通过将它包装成 cellfun
,我将它应用到元胞数组 results
中的每个元胞。现在您有一个 1x4
单元格,其中每行结果都已转换为一个单独的单元格。您只需要将其连接起来即可产生最终答案。这就是 vertcat(intermResults{:})
所做的。
你可以这样做:
m = dec2bin(1:2^numel(someStrings)-1)-'0'; %// each row contains indices of a combination
[~, s] = sort(sum(m,2)); %// compute sum of each row, sort and get sorting indices
m = m(s,:); %// sort rows according to sum
[jj, ii] = find(m.'); %'// find column indices (jj), ordered by row (ii)
result = accumarray(ii, jj, [], @(x){someStrings(x)}); %// group col indices of each row
对于您的示例,这给出了 someStrings
、
>> result
result =
{1x1 cell}
{1x1 cell}
{1x1 cell}
{1x1 cell}
{1x2 cell}
{1x2 cell}
{1x2 cell}
{1x2 cell}
{1x2 cell}
{1x2 cell}
{1x3 cell}
{1x3 cell}
{1x3 cell}
{1x3 cell}
{1x4 cell}
哪里
>> result{1}
ans =
'goat'
>> result{2}
ans =
'dog'
>> result{3}
ans =
'b'
>> result{4}
ans =
'a'
>> result{5}
ans =
'dog' 'goat'
>> result{6}
ans =
'b' 'goat'
[...]
>> result{15}
ans =
'a' 'b' 'dog' 'goat'