cheshire
cheshire

Reputation: 63

Is it possible in numpy to use advanced list slicing and still get a view?

In other words, I want to do something like

A[[-1, 0, 1], [2, 3, 4]] += np.ones((3, 3))

instead of

A[-1:3, 2:5] += np.ones((1, 3))
A[0:2, 2:5] += np.ones((2, 3))

Upvotes: 6

Views: 270

Answers (1)

Justin Peel
Justin Peel

Reputation: 47082

If I understand correctly, you can do what you want to do with the following:

A[[[-1],[0],[1]],[2,3,4]] += np.ones((3, 3))

However, the numpy folks made a function, ix_, to make it a little bit easier:

A[np.ix_([-1,0,1],[2,3,4])] += np.ones((3, 3))

I hope that helps.

Upvotes: 3

Related Questions