Alternative for nested for loop
이전 댓글 표시
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
Rik
2021년 2월 20일
You can probably use a function that generates all permutations to simplify this. Maybe even ndgrid could help you out.
채택된 답변
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Matrix Indexing에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!