How can I split the excuation of multiple for loops ?

조회 수: 1 (최근 30일)
omar th
omar th 2023년 1월 24일
댓글: omar th 2023년 1월 25일
If I have two for loops each of them has index values =5 and these loops inside let say outer for loop as illustrated in the code below.
I want to run the first loop after that run the second loop and back to the first loop to run the second index value and back again to the second loop to excute the second index value and so forth. I want show the result in this order,
X1(i)=1, X2(j)=1, X1(i)=2, X2(j)=2, X1(i)=3, X2(j)=3 ,and so on....
.....Thanks in advance for any help and comment
for k = 1:10
for i=1:5
X1(i) = do some calculation % First loop
end
for j=1:5
X2(j) = do some calculation % Second loop
end
end

채택된 답변

KSSV
KSSV 2023년 1월 24일
편집: KSSV 2023년 1월 24일
for k = 1:10
for i=1:5
X1(i) = do some calculation
X2(i) = do some calculation
end
end
  댓글 수: 3
Steven Lord
Steven Lord 2023년 1월 24일
Try it and see!
for k = 1:3 % Let's use a smaller outer loop limit
for m = 1:3 % And a smaller inner limit
X1(m) = k+m.^2
X2(m) = k+m.^3
end
end
X1 = 2
X2 = 2
X1 = 1×2
2 5
X2 = 1×2
2 9
X1 = 1×3
2 5 10
X2 = 1×3
2 9 28
X1 = 1×3
3 5 10
X2 = 1×3
3 9 28
X1 = 1×3
3 6 10
X2 = 1×3
3 10 28
X1 = 1×3
3 6 11
X2 = 1×3
3 10 29
X1 = 1×3
4 6 11
X2 = 1×3
4 10 29
X1 = 1×3
4 7 11
X2 = 1×3
4 11 29
X1 = 1×3
4 7 12
X2 = 1×3
4 11 30
omar th
omar th 2023년 1월 25일
@Steven Lord Thank you so much for your fast response. I asked the below question but I didn't get any answer and comments maybe my question wasn't clear. So, please if you have time, would you answer me or even just give your opinion ? Thanks in advance

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

태그

제품


릴리스

R2018a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by