diff doc/interpreter/stmt.txi @ 6587:00fad3bad2a5

[project @ 2007-04-26 20:23:31 by dbateman]
author dbateman
date Thu, 26 Apr 2007 20:23:31 +0000
parents 3ef1aa12f04c
children c18ed0e7ee41
line wrap: on
line diff
--- a/doc/interpreter/stmt.txi	Thu Apr 26 19:09:26 2007 +0000
+++ b/doc/interpreter/stmt.txi	Thu Apr 26 20:23:31 2007 +0000
@@ -506,6 +506,44 @@
 loop body is executed again.  This process continues until there are no
 more elements to assign.
 
+Within Octave is it also possible to iterate over matrices or cell arrays
+using the @code{for} statement. For example consider
+
+@example
+@group
+disp("Loop over a matrix")
+for i = [1,3;2,4]
+  i
+endfor
+disp("Loop over a cell array")
+for i = @{1,"two";"three",4@}
+  i
+endfor
+@end group 
+@end example
+
+@noindent
+In this case the variable @code{i} takes on the value of the columns of
+the matrix or cell matrix. So the first loop iterates twice, producing
+two column vectors @code{[1;2]}, follwed by @code{[3;4]}, and likewise
+for the loop over the cell array. This can be extended to loops over
+multidimensional arrays. For example
+
+@example
+@group
+a = [1,3;2,4]; b = cat(3, a, 2*a);
+for i = c
+  i
+endfor
+@end group 
+@end example
+
+@noindent
+In the above case, the mulitdimensional matrix @var{c} is reshaped to a
+two dimensional matrix as @code{reshape (c, rows(c),
+prod(size(c)(2:end)))} and then the same behavior as a loop over a two
+dimensional matrix is produced.
+
 Although it is possible to rewrite all @code{for} loops as @code{while}
 loops, the Octave language has both statements because often a
 @code{for} loop is both less work to type and more natural to think of.