unexpected behavior of +=

Claudio Belotti claudio.belotti at gmail.com
Fri Jun 12 05:15:27 CDT 2009


dear all,

A = zeros(2);
idx = [1;4;1;2];

A(idx) += 1
A =

   1   0
   1   1

while I was expecting to get
A =

  2 0
  1 1

how should I write the assignment to get the latter?

I got something similar if I do
[i,j] = ind2sub(idx)
A = sparse(i,j,1,2,2,'sum')

but I don't really need a sparse matrix ...


More information about the Help-octave mailing list