MATLAB for循環

您所在的位置:网站首页 菜种子大全图片价格表 MATLAB for循環

MATLAB for循環

2023-08-08 23:22| 来源: 网络整理| 查看: 265

MATLAB for循環  上一篇 下一篇  

for循環是一個重複的控製結構,可以有效地寫一個循環,需要執行特定次數。

語法:

在MATLAB中的 for循環的語法是:

for index = values ... end

值有下列形式之一:

格式 描述 initval:endval increments the index variable from initval to endval by 1, and repeats execution of program statements until index is greater than endval. initval:step:endval increments index by the value step on each iteration, or decrements when step is negative. valArray creates a column vector index from subsequent columns of array valArrayon each iteration. For example, on the first iteration, index = valArray(:,1). The loop executes for a maximum of n times, where n is the number of columns of valArray, given by numel(valArray, 1, :). The input valArray can be of any MATLAB data type, including a string, cell array, or struct. 例子 1

創建一個腳本文件,並鍵入下麵的代碼:

for a = 10:20 fprintf('value of a: %d ', a); end

當運行該文件,它會顯示以下結果:

value of a: 10 value of a: 11 value of a: 12 value of a: 13 value of a: 14 value of a: 15 value of a: 16 value of a: 17 value of a: 18 value of a: 19 value of a: 20 例子 2

創建一個腳本文件,並鍵入下麵的代碼:

for a = 1.0: -0.1: 0.0 disp(a) end

當運行該文件,它會顯示以下結果:

1 0.9000 0.8000 0.7000 0.6000 0.5000 0.4000 0.3000 0.2000 0.1000 0 例子3

創建一個腳本文件,並鍵入下麵的代碼:

for a = [24,18,17,23,28] disp(a) end

當您運行該文件,它會顯示以下結果:

24 18 17 23 28

 上一篇 下一篇  


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3