unexpected behavior of +=

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

unexpected behavior of +=

Claudio Belotti-3
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 ...
_______________________________________________
Help-octave mailing list
[hidden email]
https://www-old.cae.wisc.edu/mailman/listinfo/help-octave
Reply | Threaded
Open this post in threaded view
|

Re: unexpected behavior of +=

Jaroslav Hajek-2
On Fri, Jun 12, 2009 at 12:15 PM, Claudio
Belotti<[hidden email]> wrote:

> 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
>

This is OK. A(idx) += 1 means exactly A(idx) = A(idx) + 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 ...
>

A = accumarray (idx, 1, [2, 2]);

cheers

--
RNDr. Jaroslav Hajek
computing expert & GNU Octave developer
Aeronautical Research and Test Institute (VZLU)
Prague, Czech Republic
url: www.highegg.matfyz.cz

_______________________________________________
Help-octave mailing list
[hidden email]
https://www-old.cae.wisc.edu/mailman/listinfo/help-octave
Reply | Threaded
Open this post in threaded view
|

Re: unexpected behavior of +=

Claudio Belotti-3
On Fri, Jun 12, 2009 at 11:36, Jaroslav Hajek<[hidden email]> wrote:

> This is OK. A(idx) += 1 means exactly A(idx) = A(idx) + 1.

yes, now I understand it

> A = accumarray (idx, 1, [2, 2]);
thanks!
_______________________________________________
Help-octave mailing list
[hidden email]
https://www-old.cae.wisc.edu/mailman/listinfo/help-octave