MATLAB Answers

pedro marin

How do I create a for loop in MATLAB?

Asked by pedro marin
on 5 Mar 2012
Latest activity Edited by Patrik Ek
on 9 Apr 2015

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?



No products are associated with this question.

2 Answers

Answer by Walter Roberson
on 5 Mar 2012
 Accepted answer

The syntax is always

"for" variablename equals-sign expression

Where expression can be any expression that evaluates to a scalar, vector, matrix, or multi-dimensional array. The expression can be a function call, a variable, a list constructor in [], a numeric expression, a colon-list, or any combination of the above that is a valid expression.

The "for" loop evaluates the entire expression at the time that the flow of control enters the "for" loop from above. The entire set of values is recorded at the time the loop is entered from above. No calculations within the body of the loop can change the set of values: if a variable is mentioned in the "for" and that variable's value is changed within the loop, then the "for" loop's recorded expression list will not change.

When the "for" loop is entered from the top, the variable named in the "for" will be assigned the first value of the expression if the expression is a scalar, a row vector, or a column vector; if the expression is an array of 2 or more dimensions, then the variable will be assigned the first column of the expression, and then the statements in the loop body will be executed.

When the "end" statement of the "for" is reached normally or by way of a "continue" statement, then the loop variable will be assigned the next value of the pre-determined expression (again, the values were pre-recorded so no change in the variables mentioned in the "for" initialization will be taken in to account.) One particular point to note here is that the next pre-determined expression will always be assigned to the loop variable, even if the value of the loop variable was changed within the loop. For example,

for K = 1:4

will execute the loop body 4 times: assigning 3 to K within the loop does not cause iterations to be skipped or done "out of order".

A "continue" statement within a "for" loop causes the remaining code of the body of the loop to be skipped over (for this iteration only) and the next iteration of the loop to be started.

A "break" statement within a "for" loop causes the reaming code of the body of the loop to be skipped over (for this iteration only) and the loop to be exited at the bottom of the loop.

The loop exits at the bottom if a "break" statement is encountered, or if the loop runs out of values in the initialization expression. When the loop exits, the value of the loop variable will be whatever the last value was that was assigned to the loop variable, usually the last value of the initialization expression. However, if there was a loop such as

for K = 5
  K = 13;

then the change to K after K was given its last value from the initialization list, would be retained, leaving K as 13.

There is a loop optimization that is made in the case where the initialization is a plain colon-list . In such a case, the entire list of values is not actually generated and recorded in memory before the loop starts: instead, each value will be calculated as needed. For example compare

for K = [1 2 3 4 5 6 7 8 9 10]


for K = 1 : 10

In the first case, the initialization list is not a plain colon expression, so that vector of 1 through 10 would be stored in memory for use, whereas in the second case as an optimization MATLAB would just keep track of where it was and would generate each value as needed.

  1 Comment

Patrik Ek
on 9 Apr 2015

As far as I know the for-loop is even more excellent than what says in this text. As far as I know the matlab for-loop works similar to the for_each-loop that can be seen in many programming languages these days, or maybe the range-for in c++11. For example is this possible,

a = aObjArray;
for k = a

This is not really clear from the documentation, so I thought this would be a valuable comment.

Answer by Jan Simon
on 5 Mar 2012

You can get help from the documentation of Matlab:

doc for
help for

There you find examples and explanations.


Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi test

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

MATLAB Academy

New to MATLAB?

Learn MATLAB today!