Skip to content
Advertisement

np.where for 2d array, manipulate whole rows

I want to rebuild the following logic with numpy broadcasting function such as np.where: From a 2d array check per row if the first element satisfies a condition. If the condition is true then return the first three elements as a row, else the last three elements.

A short MWE in form of a for-loop which I want to circumvent:

JavaScript

Advertisement

Answer

If you want to use np.where:

JavaScript

output:

JavaScript

EDIT

slightly more concise version:

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