[Numpy-discussion] Smart way to do this?

santhu kumar mesanthu@gmail....
Fri Feb 22 20:38:15 CST 2013


Sorry typo :

a = np.ones(30)
idx = np.array([2,3,2]) # there is a duplicate index of 2
a[idx] += 2

On Fri, Feb 22, 2013 at 8:35 PM, santhu kumar <mesanthu@gmail.com> wrote:

> Hi all,
>
> I dont want to run a loop for this but it should be possible using numpy
> "smart" ways.
>
> a = np.ones(30)
> idx = np.array([2,3,2]) # there is a duplicate index of 2
> a += 2
>
> >>>a
> array([ 1.,  1.,  3.,  3.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,
>         1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,
>         1.,  1.,  1.,  1.])
>
>
> But if we do this :
> for i in range(idx.shape[0]):
>        a[idx[i]] += 2
>
> >>> a
> array([ 1.,  1.,  5.,  3.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,
>         1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,
>         1.,  1.,  1.,  1.])
>
> How to achieve the second result without looping??
> Thanks
> Santhosh
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.scipy.org/pipermail/numpy-discussion/attachments/20130222/c463cdc8/attachment.html 


More information about the NumPy-Discussion mailing list