FOR loop control structure

8 views (last 30 days)
richard
richard on 15 Aug 2014
Edited: per isakson on 19 Aug 2014
I have written this code:
for k=5:2:13
fprintf('The square of %d is %d.\n',k,k^2)
end
I have been trying to understand FOR loops, and trying to understand what the index (k in my example) is. Is k a row vector? When I run my script, and try to kind out what k is, matlab says it simply the number is 13, but I would expect it to be a row vector looking like:
k=[5 7 9 11 13]
Can someone please explain?

Answers (2)

Joseph Cheng
Joseph Cheng on 15 Aug 2014
k will be exactly as you say however the for loop will iteratively go through each item within k. if you put in a break point in the for loop you'll see that it doesn't do it all at once but one at a time.

per isakson
per isakson on 19 Aug 2014
Edited: per isakson on 19 Aug 2014
IMO: The description of for, Execute statements specified number of times, is not particularly good. It doesn't make justices to this powerful "Loop Control Statement". If you know basic numeric for-loops from other languages it's easy to miss the Matlab's for-loop is much more powerful. (OK, there are even more clever for-loops out there.)
Forget about "specified number of times" and think: loop over all columns (or for each column)
for item = any_type_of_array
do something with item
end
I modify your example slightly
any_type_of_array = (5:2:13);
for k = any_type_of_array
fprintf('The square of %d is %d.\n',k,k^2)
end
outputs
The square of 5 is 25.
The square of 7 is 49.
The square of 9 is 81.
The square of 11 is 121.
The square of 13 is 169.
above any_type_of_array is a row vector and the columns are scalars. Now make any_type_of_array a column vector
any_type_of_array = transpose(5:2:13);
for k = any_type_of_array
fprintf('The square of %d is %d.\n',k,k.^2)
end
outputs
The square of 5 is 7.
The square of 9 is 11.
The square of 13 is 25.
The square of 49 is 81.
The square of 121 is 169.
which is not what you wanted. (Note element-wise .^). And after that
>> k
k =
5
7
9
11
13
I guess Loren wrote a blog post on for-loops, but I cannot find one.
  2 Comments
Guillaume
Guillaume on 19 Aug 2014
Edited: Guillaume on 19 Aug 2014
Nitpick: while matlab for loop are arguably more powerful than the basic numeric for loops that you find in many languages, it's still severely limited compared to the generic enumerator/iterator interface available in just as many languages. See for example the foreach of C# that allows you to iterate over any arbitrary sequence including infinite ones.
Other than that, yes, for loops in matlab need to be understood as:
for iter = ...
for each loop iteration, iter will be a single column of ... whatever that may be (matrix, vector, cell array)
per isakson
per isakson on 19 Aug 2014
Edited: per isakson on 19 Aug 2014
Indeed!
I took the liberty to
  • replace "might be" by "are" and
  • add "basic numeric"
in my answer. (There is no strike out option.)

Sign in to comment.

Categories

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!