[Numpy-discussion] Advanced indexing advice?

Cristi Constantin darkgl0w at yahoo.com
Thu Jun 18 06:03:23 EDT 2009


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




      
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20090618/03c1856f/attachment.html>


More information about the NumPy-Discussion mailing list