[Numpy-discussion] Advanced indexing advice?

Robert kxroberto@googlemail....
Fri Jun 19 08:49:57 CDT 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@sun.ac.za>/* wrote:
> 
>     From: Stéfan van der Walt <stefan@sun.ac.za>
>     Subject: Re: [Numpy-discussion] Advanced indexing advice?
>     To: "Discussion of Numerical Python" <numpy-discussion@scipy.org>
>     Date: Thursday, June 18, 2009, 2:16 AM
> 
>     Hi Cristi
> 
>     2009/6/18 Cristi Constantin <darkgl0w@yahoo.com
>     </mc/compose?to=darkgl0w@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@scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion



More information about the Numpy-discussion mailing list