简体   繁体   中英

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. 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,:)

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) . 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM