Matlab 中while循環語句的用法
while語法:
while expression
statements
end
說明:while expression, statements, end 計算一個表達式,并在該表達式為 true 時在一個循環中重復執行一組語句。表達式的結果非空并且僅包含非零元素(邏輯值或實數值)時,該表達式為 true。否則,表達式為 false。
示例代碼如下:
function [sum] = summation(ratio, head, top)
sum = 0;
while (head <= top)
sum = sum + ratio ^ head;
head = head + 1;
end
end
假設ratio = 2,head = 0,top = 63
擴展資料:
Matlab控制流語句包括條件語句、循環和分支。
if, elseif, else 條件為 true 時執行語句。
for 用來重復指定次數的 for 循環。
parfor 并行循環。
switch, case, otherwise 執行多組語句中的一組。
try, catch 執行語句并捕獲產生的錯誤。
while 條件為 true 時重復執行的 while 循環。
break 終止執行 for 或 while 循環。
continue 將控制權傳遞給 for 或 while 循環的下一迭代。
end 終止代碼塊或指示最大數組索引。
pause 暫時停止執行 MATLAB。
return 將控制權返回給調用函數。
參考資料:
MathWorks文檔——while循環
Matlab for循環語句
示例1:
x =
0.5878 0.9511 0.9511 0.5878 0.0000 -0.5878 -0.9511 -0.9511 -0.5878 -0.0000
示例2:
array =
6 3 7 8 5 1 2 4 9 10
x1 =
0.5878 0.9511 0.9511 0.5878 0.0000 -0.5878 -0.9511 -0.9511 -0.5878 -0.0000
換一個matlab運行就可以
轉載請注明出處華閱文章網 » matlab循環語句