From bug-octave-request at bevo dot che dot wisc dot edu Tue Dec 9 23:52:41 2003 Subject: Incorrect dimensions of subarrays of ND Arrays From: "John W. Eaton" To: Quentin Spencer Cc: bug-octave at bevo dot che dot wisc dot edu Date: Tue, 9 Dec 2003 23:52:21 -0600 On 29-Nov-2003, Quentin Spencer wrote: | First, I want to thank all of those who have worked to make Nd arrays | possible. I am testing some MATLAB scripts that use ND arrays in octave | and got the following problem: | | > a=ones(4,5,6); | > size(a) | ans = | | 4 5 6 | | > size(a(1:2,:,4)) | ans = | | 2 5 1 | | In MATLAB, this returns a matrix of dimension [2,5]. The fact that this | returns a 3 dimensional array broke an assignment operation in one of my | scripts where a 2-dimensional "slice" of a 3-dimensional array was | assigned to a part of a 2-dimensional array. Please try the following patch. Thanks, jwe liboctave/ChangeLog: 2003-12-09 John W. Eaton * dim-vector.h (dim_vector::chop_trailing_singleton_dims, dim_vector::dim_vector_rep::chop_trailing_singleton_dims): New functions. * Array.cc (ArrayN::indexN): Use it. (ArrayN::index (Array&, int, const T&)): Likewise. Index: liboctave/dim-vector.h =================================================================== RCS file: /usr/local/cvsroot/octave/liboctave/dim-vector.h,v retrieving revision 1.9 diff -u -r1.9 dim-vector.h --- liboctave/dim-vector.h 24 Nov 2003 21:24:37 -0000 1.9 +++ liboctave/dim-vector.h 10 Dec 2003 05:50:55 -0000 at @ -111,6 +111,17 @@ return dims[i]; } + void chop_trailing_singletons (void) + { + for (int i = ndims - 1; i > 1; i--) + { + if (dims[i] == 1) + ndims--; + else + break; + } + } + private: // No assignment! at @ -200,7 +211,6 @@ } } - std::string str (char sep = 'x') const { OSSTREAM buf; at @ -287,6 +297,12 @@ return retval; } + + void chop_trailing_singletons (void) + { + make_unique (); + rep->chop_trailing_singletons (); + } }; static inline bool Index: liboctave/Array.cc =================================================================== RCS file: /usr/local/cvsroot/octave/liboctave/Array.cc,v retrieving revision 1.78 diff -u -r1.78 Array.cc --- liboctave/Array.cc 25 Nov 2003 05:41:35 -0000 1.78 +++ liboctave/Array.cc 10 Dec 2003 05:50:52 -0000 at @ -1859,6 +1859,7 @@ else { dim_vector new_dims; + new_dims.resize (n_dims); for (int i = 0; i < n_dims; i++) at @ -1867,6 +1868,8 @@ new_dims(i) = 1; } + new_dims.chop_trailing_singletons (); + retval = Array (new_dims); } } at @ -1885,6 +1888,8 @@ new_dims(i) = 1; } + new_dims.chop_trailing_singletons (); + retval = Array (tmp, new_dims); } else at @ -1917,6 +1922,8 @@ result_dims(1) = (ntot > 0 ? 1 : 0); } + result_dims.chop_trailing_singletons (); + retval.resize (result_dims); int n = number_of_elements (result_dims); at @ -2018,6 +2025,8 @@ dim_vector frozen_lengths = short_freeze (ra_idx, dimensions, resize_ok); + frozen_lengths.chop_trailing_singletons (); + if (frozen_lengths.length () <= n_dims) { if (all_ok (ra_idx)) Index: src/ChangeLog =================================================================== RCS file: /usr/local/cvsroot/octave/src/ChangeLog,v retrieving revision 1.894 diff -u -r1.894 ChangeLog --- src/ChangeLog 28 Nov 2003 02:50:12 -0000 1.894 +++ src/ChangeLog 10 Dec 2003 05:51:04 -0000 at @ -1,3 +1,11 @@ +2003-12-09 John W. Eaton + + * OPERATORS/op-cell.cc: Allow transpose for cell arrays. + * OPERATORS/op-m-m.cc, OPERATORS/op-cm-cm.cc, + OPERATORS/op-bm-bm.cc, OPERATORS/op-streamoff.cc, + OPERATORS/op-str-str.cc: Improve error message for attempt to + transpose N-d object. + 2003-11-27 John W. Eaton * pt-arg-list.cc (F__end__): Handle N-d objects. ------------------------------------------------------------- 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 -------------------------------------------------------------