'n' number of nested for loops
2 views (last 30 days)
Show older comments
I have a cell array M with 4 cell elements and each cell element has a 8x2 matrix.
I'm trying to making different combinations using nested for loops as
n = length(M);
[s,d] = cellfun(@size,M);
ncombs = prod(s);
P = cell(1,ncombs);
k = 0;
for i = 1:size(M{1},1)
for j = 1:size(M{2},1)
for x = 1:size(M{3},1)
for y = 1:size(M{4},1)
k = k+1;
P{k} = [M{1}(i,:);M{2}(j,:);M{3}(x,:);M{4}(y,:)];
end
end
end
end
Now in this case, I have n = 4 and hence 4 nested loops. Can I generalize this thing with any n? For example like for n = 2, 2 nested loops and correspondingly P{k} becomes [M{1}(i,:),M{2}(j,:)]
2 Comments
Matt J
on 14 Mar 2021
I have a cell array M with 4 cell elements and each cell element has a 8x2 matrix.
It doesn't make much sense for them to be a cell array in that case. You could have an 8x2x4 matrix.
Accepted Answer
More Answers (0)
See Also
Categories
Find more on Special Functions in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!