# HG changeset patch # User dbateman # Date 1177619011 0 # Node ID 00fad3bad2a50ddc55695d706491ed62617a0f5f # Parent e4ea529efab083dc9d1f1dcf0d77c9f0f3c7d7a3 [project @ 2007-04-26 20:23:31 by dbateman] diff --git a/doc/ChangeLog b/doc/ChangeLog --- a/doc/ChangeLog +++ b/doc/ChangeLog @@ -1,3 +1,8 @@ +2007-04-26 David Bateman + + * interpreter/stmt.txi: Document for loops over matrices, arrays + and cell arrays. + 2007-04-25 David Bateman * interpreter/dynamic.txi: Add additional copyrights. Add sections diff --git a/doc/interpreter/stmt.txi b/doc/interpreter/stmt.txi --- a/doc/interpreter/stmt.txi +++ b/doc/interpreter/stmt.txi @@ -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. diff --git a/src/ChangeLog b/src/ChangeLog --- a/src/ChangeLog +++ b/src/ChangeLog @@ -1,3 +1,8 @@ +2007-04-26 David Bateman + + * pt-loop.cc (tree_simple_for_command::eval (void)): Correct + reshaping of dim_vector in for loop for multi-dimensional array. + 2007-04-26 John W. Eaton * load-save.cc (find_file_to_load): Only consider regular files. diff --git a/src/pt-loop.cc b/src/pt-loop.cc --- a/src/pt-loop.cc +++ b/src/pt-loop.cc @@ -403,6 +403,7 @@ int ndims = dv.length (); for (int i = 2; i < ndims; i++) dv(1) *= dv(i); + dv.resize (2); if (dv(1) > 0) { @@ -455,6 +456,7 @@ int ndims = dv.length (); for (int i = 2; i < ndims; i++) dv(1) *= dv(i); + dv.resize (2); if (dv(1) > 0) { diff --git a/test/ChangeLog b/test/ChangeLog --- a/test/ChangeLog +++ b/test/ChangeLog @@ -1,3 +1,8 @@ +2007-04-26 David Bateman + + * test_for.m: Add tests for multi-dimensional matrices and cell + arrays. + 2007-04-04 Rafael Laboissiere * Makefile.in (clean): Also remove a.wav file created by diff --git a/test/test_for.m b/test/test_for.m --- a/test/test_for.m +++ b/test/test_for.m @@ -80,3 +80,16 @@ %! printf_assert ("\n"); %! assert(prog_output_assert("34")); +%!test +%! a = [1,3;2,4]; +%! j = 0; +%! for i = cat (3, a, 4 + a) +%! assert (i, [1;2] + 2*j++) +%! endfor + +%!test +%! a = {1,3;2,4}; +%! j=0 +%! for i = cat (3, a, cellfun(@(x) 4 + x, a, 'UniformOutput', 0)) +%! assert (i, {1 + 2*j; 2 + 2*j++}) +%! endfor