matlab for循環怎么用

有時候在使用matlab的時候,想使用for循環,怎么使用呢?怎么使用呢?怎么使用呢?下面360新知帶你一探究竟!

首先介紹一下matlab中for循環的格式,主要有兩種格式,如圖所示。文章源自四五設計網-http://www.wasochina.com/40238.html

matlab for循環怎么用-1文章源自四五設計網-http://www.wasochina.com/40238.html

通過實例來介紹for循環怎么用,首先求1到5的和,代碼:文章源自四五設計網-http://www.wasochina.com/40238.html

Y=0;文章源自四五設計網-http://www.wasochina.com/40238.html

for i=1:5文章源自四五設計網-http://www.wasochina.com/40238.html

Y=Y+i;文章源自四五設計網-http://www.wasochina.com/40238.html

end文章源自四五設計網-http://www.wasochina.com/40238.html

fprintf('Y=%d \n',Y);如圖所示:文章源自四五設計網-http://www.wasochina.com/40238.html

matlab for循環怎么用-2文章源自四五設計網-http://www.wasochina.com/40238.html

在命令行窗口按回車鍵后,可以看到得出15的結果,如圖所示。文章源自四五設計網-http://www.wasochina.com/40238.html

matlab for循環怎么用-3

也可以輸出1,0.9,0.8,......,0;的10個數,代碼如圖所示。

matlab for循環怎么用-1

在使用for循環的時候,可以使用和if語句來結合使用,break是結束循環,代碼:

for ii = 1:5;

if ii == 3;

break;

end

fprintf('ii = %d \n', ii);

end

disp('End of loop!');

可以看到輸出結果1,2,到3的時候結束循環輸出End of loop!,如圖所示:

matlab for循環怎么用-5

也可以在for循環的時候,使用continue;結束本次循環,運行結果如圖所示:

matlab for循環怎么用-6

我的微信
微信掃一掃
weinxin
我的微信
惠生活福利社
微信掃一掃
weinxin
我的公眾號
 
  • 本文由 四五設計網小助手 發表于 2024年2月22日10:16:58
  • 轉載請務必保留本文鏈接:http://www.wasochina.com/40238.html

發表評論

匿名網友
:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

拖動滑塊以完成驗證