Skip to content
Advertisement

How to use the value of a 2d array as an index to a 3d array in numpy?

I have a 2d array, A, with shape (n x m), where each element of the array at position (i,j) holds a third value k. I want to increment a 3d array with dimensions nxmxl at position (k,i,j) based on the 2d array value and position.

So for example if

JavaScript

How do you do this in numpy efficiently?

Advertisement

Answer

The question is somewhat ambiguous, but if the intent is to increment some unknown array B at indices (0,0,0), (1,0,1), (3,1,0), and (3,1,1), then the following should be fine:

JavaScript

For example:

JavaScript

Another way of doing the same thing is:

JavaScript
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement