David
David

Reputation: 1174

python array slice, what happens here?

I'm stuck on a problem regarding python array slicing.

I have 2 numpy.ndarrays:

img is a 1d array (256 length)

optimised is an 2d array (231x50)

what happens here? how is result made up?

result = img[optimised.astype('uint8')]  # result is a 2d 231x50 array

Is there a equivalent in javascript?

Upvotes: 0

Views: 85

Answers (1)

V. Ayrat
V. Ayrat

Reputation: 2719

This example should clarify what happens. Each value from a used as index in b and put in the same place. So a[0, 0] is 1 and b[1] is 28 so in resulting array [0, 0] element will be 28.

>>> a
array([[1, 0],
       [0, 1]])
>>> b
array([13, 28])
>>> b[a]
array([[28, 13],
       [13, 28]])

Upvotes: 1

Related Questions