Ruan
Ruan

Reputation: 189

Creating array from existing array based on conditions of other arrays

itmPaths is a list with 719255 (integer) values.
Pt is a 719255x1 matrix/array with float64 values.
C is a 719255x1 matrix/array with float64 values.

I would like to extract the index values where Pt > C, and then use those index values to extract the values from itmPaths that correspond with those index values, and then store those values in a new array, called exPaths. I have tried using the following code:

    exPaths = itmPaths[index for index,value in enumerate(Pt-C) if value > 0]

In Matlab I can successfully do this using:

    exPaths = itmPaths(Pt>C);

I would like to keep the code as efficient as possible. Thanks.

Upvotes: 0

Views: 42

Answers (1)

azro
azro

Reputation: 54148

Using a list comprehension, you could do this, but just as I don't know the exact structure of what you call matrix you may adapt, but zipping both allows to keep track of the index (to extract value after) and the values (to apply the condition)

exPaths = [itmPaths[idx] for idx, pc in enumerate(zip(Pt, C)) if pc[0] > pc[1]]

Upvotes: 1

Related Questions