[SciPy-user] numpy indexing with arrays

Pauli Virtanen pav@iki...
Wed Dec 10 06:32:24 CST 2008


Wed, 10 Dec 2008 12:26:01 +0000, Jeremy Sanders wrote:

> Hi - I've been trying to think of a way of doing this in numpy. Is it
> possible without an explicit loop?
> 
> I have an array of values
> 
> vals = [1.1, 2.2, 3.3...]
> 
> I also have a 2D array of indices into this vals array:
> 
> img = [[1,2,3],
>        [2,3,4],
>        ..]
> 
> Can I make another image with values from vals as indexed by the
> indicies in img? i.e.,
> 
> img = [[1.1, 2.2, 3.3],
>        [2.2, 3.3, 4.4],
>        ...]

Yes,

>>> import numpy as np
>>> vals = np.array([1.1, 2.2, 3.3, 4.4])
>>> img = np.array([[0,1,2],[1,2,3]])
>>> vals[img]
array([[ 1.1,  2.2,  3.3],
       [ 2.2,  3.3,  4.4]])

See

	http://docs.scipy.org/doc/numpy/user/basics.indexing.html
	http://docs.scipy.org/doc/numpy/reference/arrays.indexing.html



More information about the SciPy-user mailing list