简体   繁体   English

如何在单行中将Matlab函数max(y(x> 3)> 2)转换为numpy

[英]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. 我想将所有,任何,最大最小值之类的matlab函数转换为numpy。

since these functions takes compound expressions as inputs, wanted to know how to achive the same in numpy. 由于这些函数将复合表达式作为输入,因此想知道如何在numpy中实现相同的表达式。

for ex. 对于前。 max(y(x>3)>2) 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 在matlab中我会得到答案为9

how can i write the above max function in numpy so that it accepts the diffrent expressions. 我如何在numpy中编写上述max函数,以便它接受不同的表达式。

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. 非常感谢您的提前输入。

In general ( http://wiki.scipy.org/NumPy_for_Matlab_Users ) is a very good guide for making the conversion from MATLAB -> numpy 一般来说( http://wiki.scipy.org/NumPy_for_Matlab_Users )是从MATLAB-> numpy进行转换的很好的指南

x = np.array(x)
np.max(x[x>3])
np.max(x[(x>3)*(x>5)])
np.max(x[(x>3)*(x<6)])

不是那么短,而是一线:

max([y[i] for i in range(len(y)) if x[i] > 3 and y[i]>2])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM