Sunday, 15 April 2012

python - Indexing in numpy array -



python - Indexing in numpy array -

given lookup table:

colors = [ [0,0,0],\ [0, 255, 0],\ [0, 0, 255],\ [255, 0, 0]]

and index numpy matrix input 2x2:

a = np.array([[0,1],[1,1]])

how can map a 2x2x3 matrix b, b[i][j] = colors[a[i][j]]? want avoid using loop here.

have tried:

colors[a]

here's total example:

import numpy np colors = np.array([[0,0,0], [0, 255, 0], [0, 0, 255], [255, 0, 0] ]) = np.array([[0, 1], [1, 1]]) new = colors[a] new.shape # (2, 2, 3) new # array([[[ 0, 0, 0], # [ 0, 255, 0]], # # [[ 0, 255, 0], # [ 0, 255, 0]]])

python numpy

No comments:

Post a Comment