From bug-octave-request at bevo dot che dot wisc dot edu Thu Dec 11 23:09:47 2003 Subject: more indexing weirdness From: "John W. Eaton" To: Paul Kienzle Cc: bug-octave at bevo dot che dot wisc dot edu Date: Thu, 11 Dec 2003 23:09:38 -0600 On 11-Dec-2003, Paul Kienzle wrote: | octave-2.1.52, Debian linux. | | Shouldn't the following add 5? | | octave:24> x=eye(2); x([1,1,1,1,1])+=1 | x = | 2 0 | 0 1 | | octave:25> x=[1:4]; x([1,1,1,1,1])+=1 | x = 2 2 3 4 No, because this expression is equivalent to x([1,1,1,1,1]) = x([1,1,1,1,1]) + 1 for x = [1:4], the RHS evaluates to [ 2 2 2 2 2 ] It is equivalent to writing [x(1), x(1), x(1), x(1), x(1)] + 1 If you do something like x([1,1,1,1,1]) = [ 2 2 2 2 2 ] then you copy the RHS elements to the corresponding element of X. It is equivalent to writing x(1) = 2 x(1) = 2 x(1) = 2 x(1) = 2 x(1) = 2 I know there are other possible interpretations of what this syntax could mean, but I think this is what Matlab does. (I know, it does not have +=, but if we agree that x OP= y is equivalent to x = x OP y, then I think this is how things must work.) jwe ------------------------------------------------------------- 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 -------------------------------------------------------------