Skip to content
Advertisement

3d numpy array perform operation along column i only

I have a 3d numpy array like this (as an example)

JavaScript

I want to apply the following operations only to elements within the column with index 1 in the inner dimension. The elements are [-2,6,10,14] for the example above. The operations would be:

JavaScript

Can someone help me? I have looked into several NumPy methods but can’t seem to adapt to my example.

Advertisement

Answer

You need to change you function to be vectorial (i.e accept an array and input and return an array as output), and slice to only apply it on the desired “column”:

JavaScript

output:

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