[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: unexpected behavior of +=
From: |
Jaroslav Hajek |
Subject: |
Re: unexpected behavior of += |
Date: |
Fri, 12 Jun 2009 12:36:46 +0200 |
On Fri, Jun 12, 2009 at 12:15 PM, Claudio
Belotti<address@hidden> 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