MATLAB Answers

abdel m
0

how can i use several loops

abdel m
さんによって質問されました 2019 年 5 月 9 日
最新アクティビティ abdel m
さんによって 編集されました 2019 年 5 月 11 日
Hi,
Can anyone explain to me how matlab read this loops
for k=1:nt-1
for i=1:nx
end (for i)
for i=nx:1
end (for i=nx:1)
end (for k)
- For example for k=1, matlab will start by i=1:nx (first loop for i)or it will pass to i=1 only.after, it will pass to i=nx (second loop) ?

  0 件のコメント

サインイン to comment.

タグ

3 件の回答

James Tursa
回答者: James Tursa
2019 年 5 月 9 日
編集済み: James Tursa
2019 年 5 月 9 日
 採用された回答

MATLAB will do all of the loops in the order it encounters them. So for the k=1 iteration it will do the i=1:nx loop in its entirety and then do the i=nx:1 loop in its entirety. Then it will do the k=2 iteration and do both inner loops in their entirety again. For every iteration of k, both inner loops will be done in their entirety.
Side Note: If nx>1, that i=nx:1 loop won't do anything. Maybe i=nx:-1:1 was meant?

  0 件のコメント

サインイン to comment.


gonzalo Mier
回答者: gonzalo Mier
2019 年 5 月 9 日

I will try to explain as best as I can. To do that, I will give values to the variables. nt = 4, nx = 5:
for k=1:3 ( = [1,2,3])
k is executed 3 times
disp(" k = "+k);
for i=1:5 ( = [1,2,3,4,5])
i is executed 5 times for each k (3*5 = 15 times)
disp(" i = "+i);
end
for j=5:1 ( = [])
Not executed as 5:1 is an empty vector.
To do it in the inverse way, you should write 5:-1:1
disp(" j = "+j);
end
end
So the output in the screen is:
k = 1
i = 1
i = 2
...
i = 5
k = 2
i = 1
i = 2
...
i = 5
k = 2
i = 1
i = 2
...
i = 5

  0 件のコメント

サインイン to comment.


abdel m
回答者: abdel m
2019 年 5 月 11 日
編集済み: abdel m
2019 年 5 月 11 日

How can i do only the first iteration in the first loop, then pass to second loop and do the first iteration. is it possible?

  0 件のコメント

サインイン to comment.



Translated by