Mercurial > hg > octave-lojdl
changeset 6587:00fad3bad2a5
[project @ 2007-04-26 20:23:31 by dbateman]
author | dbateman |
---|---|
date | Thu, 26 Apr 2007 20:23:31 +0000 |
parents | e4ea529efab0 |
children | 4deaf99400b2 |
files | doc/ChangeLog doc/interpreter/stmt.txi src/ChangeLog src/pt-loop.cc test/ChangeLog test/test_for.m |
diffstat | 6 files changed, 68 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- a/doc/ChangeLog +++ b/doc/ChangeLog @@ -1,3 +1,8 @@ +2007-04-26 David Bateman <dbateman@free.fr> + + * interpreter/stmt.txi: Document for loops over matrices, arrays + and cell arrays. + 2007-04-25 David Bateman <dbateman@free.fr> * interpreter/dynamic.txi: Add additional copyrights. Add sections
--- 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.
--- a/src/ChangeLog +++ b/src/ChangeLog @@ -1,3 +1,8 @@ +2007-04-26 David Bateman <dbateman@free.fr> + + * 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 <jwe@octave.org> * load-save.cc (find_file_to_load): Only consider regular files.
--- 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) {
--- a/test/ChangeLog +++ b/test/ChangeLog @@ -1,3 +1,8 @@ +2007-04-26 David Bateman <dbateman@free.fr> + + * test_for.m: Add tests for multi-dimensional matrices and cell + arrays. + 2007-04-04 Rafael Laboissiere <rafael@debian.org> * Makefile.in (clean): Also remove a.wav file created by
--- 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