From for to While loop

2 ビュー (過去 30 日間)
Astals
Astals 2020 年 11 月 24 日
編集済み: Astals 2020 年 11 月 24 日
Hello! Could someone please help me to convert this for loop to a while loop?

採用された回答

Rik
Rik 2020 年 11 月 24 日
Your original for-loop probably was incorrect. And why do you want a while loop? You already know how many iterations you want.
A = (1:10)';
B= rand(10,2);
C= A; C(:,2:3)= B;
Y= B(:,2);
X= B(:,1);
for i= 1:size(B,1)
% ^ are you sure that shouldn't be C instead?
C(i,4)= sin(C(i,3)/sin(C(i,2)));
end
In this case you can avoid the loop entirely:
C(:,4)=sin( C(:,3) ./ sin( C(:,2) ) );
And are you sure you mean this, and not this?
C(:,4)=sin( C(:,3) ) ./ sin( C(:,2) );

その他の回答 (1 件)

Steve Eddins
Steve Eddins 2020 年 11 月 24 日
Here is one way to convert a typical MATLAB for loop to a while loop:
for i = 1:N
...
end
i = 1;
while i <= N
...
i = i+1;
end
Documentation links: for loop, while loop
  2 件のコメント
Steve Eddins
Steve Eddins 2020 年 11 月 24 日
Can you show us specifically what you tried?
Rik
Rik 2020 年 11 月 24 日
Now deleted comment:
I tried that, but I couldn't get the whole matrix to show.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by