FOR loop control structure

3 次查看(过去 30 天)
richard
richard 2014-8-15
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?

回答(2 个)

Joseph Cheng
Joseph Cheng 2014-8-15
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 2014-8-19
编辑:per isakson 2014-8-19
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 个评论
Guillaume
Guillaume 2014-8-19
编辑:Guillaume 2014-8-19
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 2014-8-19
编辑:per isakson 2014-8-19
Indeed!
I took the liberty to
  • replace "might be" by "are" and
  • add "basic numeric"
in my answer. (There is no strike out option.)

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by