[Numpy-discussion] Advanced indexing advice?

Stéfan van der Walt stefan at sun.ac.za
Thu Jun 18 05:16:15 EDT 2009


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



More information about the NumPy-Discussion mailing list