# How do I create a for loop in MATLAB?

4,279 次查看（过去 30 天）
pedro marin2012-3-5

I am completely lost in for loops, I just don't get it. The book and my professor haven't helped much. Where can I get help?
##### 2 个评论显示 1更早的评论隐藏 1更早的评论
Walter Roberson 2017-5-14

### 采纳的回答

Walter Roberson 2012-3-5

A basic for loop in MATLAB is often used to assign to or access array elements iteratively. For example, let’s say you have a vector A, and you want to simply display each value one at a time:
A = [3 6 9 4 1];
for i = 1:length(A)
disp(A(i))
end
For more examples using for loops, see:
##### 5 个评论显示 4更早的评论隐藏 4更早的评论
Walter Roberson 2022-9-21

A = randi([0 9], 1, 20)
A = 1×20
1 9 5 8 6 2 6 2 2 0 3 2 1 4 7 3 9 7 2 6
%then output in groups of 5 in vectorized form
reshape(A, 5, []).'
ans = 4×5
1 9 5 8 6 2 6 2 2 0 3 2 1 4 7 3 9 7 2 6
%or if you need a for loop
for K = 1 : 5 : numel(A); disp(A(K:K+4)); end
1 9 5 8 6 2 6 2 2 0 3 2 1 4 7 3 9 7 2 6
Matters get more complicated if the number of entries in the array is not a multiple of the number of columns you want to use. For example,
A = randi([0 9], 1, 23)
A = 1×23
9 6 1 5 7 4 8 4 8 4 4 5 8 5 3 6 9 2 1 4 8 9 9
for K = 1 : 5 : numel(A); disp(A(K : min(end,K+4))); end
9 6 1 5 7 4 8 4 8 4 4 5 8 5 3 6 9 2 1 4 8 9 9

### 更多回答（7 个）

Jan 2012-3-5
You can get help from the documentation of Matlab:
doc for
help for
There you find examples and explanations.
##### 1 个评论显示 无隐藏 无
Meenakshi Bhardwaj 2018-7-19
Thanks so much. Finally, I understood what is for loop.

Jan Afridi 2017-9-29
For loop repeat itself for a given number of input. The syntax for “For Loop Matlab” is
for variable = expression
Program Statement
end
In the above syntax, the expression has one of the following forms.
Initial value : Final value
for x = 1:10
fprintf('value of x: %d\n', x);
end
Initial value : Step : Final value
for x = 1:2:10
fprintf('value of x: %d\n', x);
end
Value Array
for x = [1 4 6 8 90]
disp(x)
end
##### 0 个评论显示 -1更早的评论隐藏 -1更早的评论

mohamed mohamed 2021-2-6

for x = 1:10
fprintf('value of x: %d\n', x);
end
value of x: 1 value of x: 2 value of x: 3 value of x: 4 value of x: 5 value of x: 6 value of x: 7 value of x: 8 value of x: 9 value of x: 10
##### 0 个评论显示 -1更早的评论隐藏 -1更早的评论

Narasimman P 2021-7-30
for a=1:10
end
##### 2 个评论显示 1更早的评论隐藏 1更早的评论
Walter Roberson 2021-11-17
The code posted by @Narasimman P is a completely valid for loop, just one that does not do anything inside the loop. All it does is count from 1 to 10 internally. After the loop, two things will have changed:
1. Time will have elapsed, which could be important if you are waiting for something to happen
2. The loop control variable 'a' will have the same value as it was last assigned, so in this case after the loop 'a' will have the double precision value 10 .
disp('before')
before
whos
disp('starting loop')
starting loop
for a=1:10
end
disp('after')
after
whos
Name Size Bytes Class Attributes a 1x1 8 double
So there has been output: the variable a did not exist before, and after the loop it does exist.

Manan Shah 2022-5-8

for i = 0:8 ;
a = pow10 (i);
disp a(i);
end
##### 1 个评论显示 无隐藏 无
Walter Roberson 2022-5-8
disp a(i)
would mean the same thing as
disp('a(i)')
You probably want
disp(a(i))

Iosif 2022-11-13

D=input ('Βαθος νερου σε m ')
W=input ('Βαρος ανα μοναδα μηκους της γραμμης αγκυρωσης στο νερο σε N/m ')
Hex=input ('εξωτερικη φορτηση σε kn ')
dx=input ('οριζοντια μετατοπιση σε m ')
if dx/D>=0.3 && dx/D<=0.6
else
disp ('Δωσε διαφορετικη τιμη για το dx')
dx=input ('οριζοντια μετατοπιση σε m ')
end
I want to make my programm go to if after else and run that lines again until if line is satisfied
##### 1 个评论显示 无隐藏 无
Walter Roberson 2022-11-15
while ~isnumeric(dx) || ~isscalar(dx) || dx/D<0.3 || dx/D>0.6
disp ('Δωσε διαφορετικη τιμη για το dx')
dx=input ('οριζοντια μετατοπιση σε m ')
end

Kartick 2023-1-11

There are 4 type of loops: while, for, if and case.
For loop :
Eg: you have your robot whom you wish to give command to walk 100steps. The command will be
for steps=1:100
end
disp(steps)
100
The robot will go 100steps and stop and output will be displayed as 100 after completion.
For loop is used to solve many mathematical problems like factorials etc.
##### 1 个评论显示 无隐藏 无
Walter Roberson 2023-1-11
Computer Scientists use the term "control statement" for code structures that will execute selectively exactly zero or one time. Computer Scientists use the term "loop" for code structures that have the potential to execute more than one time. "if" and "case" are control structures but are not loops.
If you imagine the execution point as starting from the "top" and falling downward, then "if" and "case" only ever have the execution point continuing to fall downwards, whereas "for" and "while" in general require pumping the execution point back up again.

### 类别

Find more on Loops and Conditional Statements in Help Center and File Exchange

### Community Treasure Hunt

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

Start Hunting!

Translated by