Home  >  Article  >  How to use matlab loop statement for

How to use matlab loop statement for

藏色散人
藏色散人Original
2020-02-20 09:38:4463974browse

How to use matlab loop statement for

#How to use matlab loop statement for?

How to use for statement and application examples in matlab

for loop statement

1. The general format is:

for x (loop variable ) = array (array)

commands (executed loop code)

end

2. array can be a number or an array, for example, input:

for a=5
for a=1:5
for a=1:1:5(以1为步长到5)

Only when a=1:5 and a=1:1:5, the results between them will be displayed. When a=5, only the final result will be displayed. The change of a is that a=1 in the first cycle, a=2 in the second cycle, a=3 in the third cycle, a=4 in the fourth cycle, and a=5 in the fifth cycle.

3. (commands) is a command, and there can be many command lines. The most common one is to call the a change mentioned above. For example:

for a=10 %循环10次
s=a+1 %循环语句
end %结束

The a above does not need to be specified, a The change is that as mentioned above, it is first 1, then 2, 3,... Then s is first s=1 1=2, then s=2 1=3, s=3 1=4,..., s=10 1= 11. The end of the loop is an instruction to continuously add s, and finally =11.

4. The for statement can be nested, just like C

for a=5 %第一个for循环
for b=5 %第二个for循环
s=a+b
end %第二个for结束
end %第一个for结束

Because the last loop a=5, b=5, so 5 5=10.

Example 1,

for a= (1:10)’
s=a+1
end

The function of ' is inversion, so a becomes a 10:1 column vector, a=array, s=array.

For more programming related content, please pay attention to the Programming Introduction column on the php Chinese website!

The above is the detailed content of How to use matlab loop statement for. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn