From bug-octave-request at bevo dot che dot wisc dot edu Thu Feb 5 14:51:56 2004 Subject: size() with two arguments fails for Nd arrays From: "John W. Eaton" To: Quentin Spencer Cc: bug-octave at bevo dot che dot wisc dot edu Date: Thu, 5 Feb 2004 14:50:28 -0600 On 26-Jan-2004, Quentin Spencer wrote: | The function size(A,N) should return the size of the Nth dimension. | However, it returns an error for N>2, although it functions correctly | for Nd arrays when only one argument is given. Example: | | octave:1> x=ones(2,3,4); | octave:2> size(x) | ans = | | 2 3 4 | | octave:3> size(x,2) | ans = 3 | octave:4> size(x,3) | error: size: invalid second argument -- expecting 1 or 2 | | Also note that the help info for "size" is has not changed since Nd | arrays were implemented and needs to be updated as well. Please try the following patch. Thanks, jwe 2004-02-05 John W. Eaton * data.cc (Fsize): Update for N-d objects. Index: src/data.cc =================================================================== RCS file: /usr/local/cvsroot/octave/src/data.cc,v retrieving revision 1.118 diff -u -r1.118 data.cc --- src/data.cc 5 Feb 2004 18:53:18 -0000 1.118 +++ src/data.cc 5 Feb 2004 20:48:35 -0000 at @ -792,9 +792,9 @@ Return the number rows and columns of at var{a} dot \n\ \n\ With one input argument and one output argument, the result is returned\n\ -in a 2 element row vector. If there are two output arguments, the\n\ -number of rows is assigned to the first, and the number of columns to\n\ -the second. For example,\n\ +in a row vector. If there are multiple output arguments, the number of\n\ +rows is assigned to the first, and the number of columns to the second,\n\ +etc. For example,\n\ \n\ at example\n\ at group\n\ at @ -807,8 +807,8 @@ at end group\n\ at end example\n\ \n\ -If given a second argument of either 1 or 2, at code{size} will return\n\ -only the row or column dimension. For example\n\ +If given a second argument, at code{size} will return the size of the\n\ +corresponding dimension. For example\n\ \n\ at example\n\ size ([1, 2; 3, 4; 5, 6], 2)\n\ at @ -852,12 +852,12 @@ error ("size: expecting scalar as second argument"); else { - if (nd == 1) - retval(0) = args(0).rows (); - else if (nd == 2) - retval(0) = args(0).columns (); + dim_vector dv = args(0).dims (); + + if (nd > 0 && nd <= dv.length ()) + retval(0) = dv(nd-1); else - error ("size: invalid second argument -- expecting 1 or 2"); + error ("size: requested dimension (= %d) out of range", nd); } } else ------------------------------------------------------------- Octave is freely available under the terms of the GNU GPL. Octave's home on the web: http://www.octave.org How to fund new projects: http://www.octave.org/funding.html Subscription information: http://www.octave.org/archive.html -------------------------------------------------------------