[Numpy-discussion] Advanced indexing advice?

Robert kxroberto at googlemail.com
Fri Jun 19 09:49:57 EDT 2009


well its not really slow.
yet with np.where it seems to be 2x faster for large arrays :

a[1:4,1:4] = np.where(mask,b,a[1:4,1:4])

otherwise consider Cython: 
http://docs.cython.org/docs/numpy_tutorial.html#tuning-indexing-further


Robert


Cristi Constantin wrote:
> 
> Thank you so much for your prompt answer, Stéfan.
> It's a very very interesting method. I will keep it for future. :)
> 
> But, i tested it with a few examples and the speed of execution is just 
> a tiny bit slower than what i told you i was using. So it's not faster, 
> it's about the same speed.
> 
> Thank you again. I will play with your method a little more.
> 
> --- On *Thu, 6/18/09, Stéfan van der Walt /<stefan at sun.ac.za>/* wrote:
> 
>     From: Stéfan van der Walt <stefan at sun.ac.za>
>     Subject: Re: [Numpy-discussion] Advanced indexing advice?
>     To: "Discussion of Numerical Python" <numpy-discussion at scipy.org>
>     Date: Thursday, June 18, 2009, 2:16 AM
> 
>     Hi Cristi
> 
>     2009/6/18 Cristi Constantin <darkgl0w at yahoo.com
>     </mc/compose?to=darkgl0w at yahoo.com>>:
>      > I have a question about advanced indexing.
>      >
>      > I have 2 matrices :
>      >
>      >>>>
>      > a=array([[ 0,  1,  2,  3,  4,  5],
>      >             [ 6,  7,  8,  9, 10, 11],
>      >          [12, 13, 14, 15, 16, 17],
>      >          [18, 19, 20, 21, 22, 23]])
>      >
>      > b=array([[1, 0, 1],
>      >          [0, 2, 0],
>      >          [0, 0, 3]])
>      >>>>
>      >
>      > I want to put all NON-zero elements from array B into array A,
>     but use
>      > offset!
> 
>     Here's a solution using views:
> 
>     offset = np.array([1,1])
>     slices = [slice(*x) for x in zip(offset, offset + b.shape)]
>     c = a[slices]
>     mask = (b != 0)
>     c[mask] = b[mask]
> 
>     Regards
>     Stéfan
> 
> 
> 
> ------------------------------------------------------------------------
> 
> _______________________________________________
> Numpy-discussion mailing list
> Numpy-discussion at scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion




More information about the NumPy-Discussion mailing list