Skip to content
Advertisement

how to translate matlab function max(y(x>3)>2) to numpy in single line

i want to convert matlab functions like all, any, max min to numpy.

since these functions takes compound expressions as inputs, wanted to know how to achive the same in numpy.

for ex. max(y(x>3)>2)

x = [1,2,3,4,5,6]
y = [2,4,6,8,9,2]

in matlab i would have got the answer as 9

how can i write the above max function in numpy so that it accepts the diffrent expressions.

like

max(x>3) - ans 6
max((x>3)>5) - ans 6
max((x>3) & (x<6)) - ans 5

thanks a lot for your inputs in advance.

Advertisement

Answer

Not so short, but one-liner:

max([y[i] for i in range(len(y)) if x[i] > 3 and y[i]>2])
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement