Alternative for nested for loop
Show older comments
I need to implement this logic to make combinations of a matrix. This code is a bit cumbersome since I need to add more rows and columns at some point. Is there a simpler way to do this?
k = 0;
for x1 = 1:2
for x2 = 1:2
for x3 = 1:2
for y1 = 1:2
for y2 = 1:2
for y3 = 1:2
for z1 = 1:2
for z2 = 1:2
for z3 = 1:2
k = k+1;
JV{k} = [J1(1,x1),J2(1,x2),J3(1,x3);
J1(2,y1),J2(2,y2),J3(2,y3);
J1(3,z1),J2(3,z2),J3(3,z3)];
end
end
end
end
end
end
end
end
end
J1, J2 & J3 are 3x2 matrices.
1 Comment
Rik
on 20 Feb 2021
You can probably use a function that generates all permutations to simplify this. Maybe even ndgrid could help you out.
Accepted Answer
More Answers (0)
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!