FOR loop control structure

조회 수: 6 (최근 30일)
richard
richard 2014년 8월 15일
편집: per isakson 2014년 8월 19일
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