[英]How to loop over cell with different array size?
The following code supposed to output the content of 1st cell in combs and access to each row of the cell which is defined by bb. 以下代码应该以梳状输出第一个单元格的内容并访问由bb定义的单元格的每一行。 My issue here is that it doesn't loop over to 2nd and 3rd cell. 我的问题是它不会循环到第二和第三单元格。
% input arrays
A=[2 1 3];
B=[4 2 1 3 3];
C=[1 3];
D=[3 2 4 2 1 1];
E=[4 1 1];
% possible subsets of a set
combs = arrayfun(@(x) nchoosek({A,B,C,D,E},x),3:numel({A,B,C,D,E}),'Uniform',0);
for j=1:numel(combs)
aa=combs{j}
for g=1:numel(aa)
bb=aa(g,:)
end
end
It only loop for the first cell array then this error message occur: 它只循环第一个单元格数组,然后会出现此错误消息:
Index exceeds matrix dimensions. 索引超出矩阵尺寸。
Error in simtt1 (line 18) bb=aa(g,:) simtt1(第18行)中的错误bb = aa(g,:)
May I know how to make it loop for 2nd cell and 3rd cell? 我可以知道如何使其循环到第二个单元格和第三个单元格吗?
Since that iterator g
is used to index into the rows of aa
, you need to iterate it from 1 to size(aa,1)
. 由于该迭代器g
用于索引aa
的行,因此需要将其从1 to size(aa,1)
迭代1 to size(aa,1)
。 Thus, make this edit in your code - 因此,请在您的代码中进行此修改-
for g=1:size(aa,1)
It worked for the outer loop iterator - for j=1:numel(combs)
, because combs
is a 1D
cell array. 它适用于外循环迭代器for j=1:numel(combs)
,因为combs
是一1D
单元数组。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.