From bug-request at octave dot org Tue Oct 19 02:06:10 2004 Subject: Re: N-dim slice produces incorrect result From: Tom Brown To: "John W. Eaton" Cc: octave bug mailing list Date: Tue, 19 Oct 2004 00:59:03 -0500 On Mon, Oct 18, 2004 at 01:34:34PM -0400, John W. Eaton wrote: > On 17-Oct-2004, Tom Brown wrote: > | To: bug-octave at bevo dot che dot wisc dot edu > | Cc: tdbrown > | Subject: N-dim slice produces incorrect result > | > | Bug report for Octave 2.1.60 configured for i386-pc-linux-gnu > | > | Description: > | ----------- > | > | It seems that taking a slice of a multidimensional (N-dim) matrix does not work > | as expected. Perhaps it has not been implemented. I think the current output > | is not correct and therefore believe this to be a bug. > | > | Repeat-By: > | --------- > | > | inmat(:,:,1) = [1,2,3;4,5,6;7,8,9];inmat(:,:,2) = inmat(:,:,1) + 9; inmat(:,:,3) = inmat(:,:,1) + 18 > | outmat = zeros(3,3); > | outmat(:) = inmat(1,:,:) > | or > | outmat(:,:) = inmat(1,:,:) > | > | I get > | 1 2 3 > | 2 3 10 > | 3 10 11 > | > | I was expecting > | 1 10 19 > | 2 11 20 > | 3 12 21 > | > | Fix: > | --- > | > | For my needs this is a work around: > | outmat = reshape(inmat(1,:,:),[3,3]) > > Please try the following patch. > > 2004-10-18 John W. Eaton > > * Array.cc (assign2): Save result of squeeze operation. > Squeeze if ndims is > 2, not if length of RHS vector is > 2. The patch applied to debian octave 2.1.60-1 fixed my problem in the inital bug report, but I have found other seemingly-incorrect behavior with N-dim matrix assignment. Here is what I ran: inmat(:,:,1) = [1,2,3;4,5,6;7,8,9];inmat(:,:,2) = inmat(:,:,1) + 9;inmat(:,:,3) = inmat(:,:,1) + 18; outmat = zeros(3,3,3); outmat(:,1,:) = inmat(1,:,:) % Makes an assignment outmat(:,:) = inmat(1,:,:) % Outputs an error, I would not expect outmat to change outmat % but it has changed Here is the output with blank lines removed: octave:1> inmat(:,:,1) = [1,2,3;4,5,6;7,8,9];inmat(:,:,2) = inmat(:,:,1) + 9;inmat(:,:,3) = inmat(:,:,1) + 18; octave:2> outmat = zeros(3,3,3); octave:3> outmat(:,1,:) = inmat(1,:,:) % Makes an assignment outmat = ans(:,:,1) = 1 0 0 2 0 0 3 0 0 ans(:,:,2) = 10 0 0 11 0 0 12 0 0 ans(:,:,3) = 19 0 0 20 0 0 21 0 0 octave:4> outmat(:,:) = inmat(1,:,:) % Outputs an error, I would not expect outmat to change error: A(IDX-LIST) = X: X must be a scalar or size of X must equal number of elements indexed by IDX-LIST error: assignment failed, or no method for `matrix = matrix' error: evaluating assignment expression near line 4, column 13 octave:4> outmat % but it has changed outmat = ans(:,:,1) = 9.6572e+05 1.4263e+73 5.6310e+175 7.2659e+223 7.2277e+159 6.0598e+233 1.2253e-95 1.3809e-94 1.8055e+219 ans(:,:,2) = 0.0000e+00 0.0000e+00 3.3952e-313 0.0000e+00 2.9078e-312 6.4377e-314 0.0000e+00 9.6578e+05 3.3952e-313 ans(:,:,3) = 4.2440e-314 2.0000e+01 0.0000e+00 1.8886e-312 2.1000e+01 0.0000e+00 9.6576e+05 0.0000e+00 0.0000e+00 If I run the same commands without the comments I get slightly different incorrect output. octave:1> inmat(:,:,1) = [1,2,3;4,5,6;7,8,9];inmat(:,:,2) = inmat(:,:,1) + 9;inmat(:,:,3) = inmat(:,:,1) + 18; octave:2> outmat = zeros(3,3,3); octave:3> outmat(:,1,:) = inmat(1,:,:) outmat = ans(:,:,1) = 1 0 0 2 0 0 3 0 0 ans(:,:,2) = 10 0 0 11 0 0 12 0 0 ans(:,:,3) = 19 0 0 20 0 0 21 0 0 octave:4> outmat(:,:) = inmat(1,:,:) error: A(IDX-LIST) = X: X must be a scalar or size of X must equal number of elements indexed by IDX-LIST error: assignment failed, or no method for `matrix = matrix' error: evaluating assignment expression near line 4, column 13 octave:4> outmat outmat = ans(:,:,1) = 9.6572e+05 6.3660e-314 5.1221e-267 0.0000e+00 1.2944e-312 3.3952e-313 0.0000e+00 4.6054e-312 7.1673e-316 ans(:,:,2) = 10 0 0 11 0 0 12 0 0 ans(:,:,3) = 19 0 0 20 0 0 21 0 0 I ran these two sequences of commands on the 2.1.60-1 i386 binary in debian unstable. It also outputs seemingly random numbers in both cases. Thank you for making a patch for my problem so quickly. Good luck with the new bug. Tom -- 28 70 20 71 2C 65 29 61 9C B1 36 3D D4 69 CE 62 4A 22 8B 0E DC 3E mailto:tdbrown at uiuc dot edu http://thecap.org/ ------------------------------------------------------------- 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 -------------------------------------------------------------