在numpy/scipy中找到函数matlab [英] find function matlab in numpy/scipy

查看:153
本文介绍了在numpy/scipy中找到函数matlab的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有numpy/scipy的matlab中的find(A>9,1)等效功能.我知道numpy中有nonzero函数,但我需要的是第一个索引,以便可以在另一个提取的列中使用第一个索引.

Is there an equivalent function of find(A>9,1) from matlab for numpy/scipy. I know that there is the nonzero function in numpy but what I need is the first index so that I can use the first index in another extracted column.

例如:A = [ 1 2 3 9 6 4 3 10 ] find(A>9,1)将在matlab中返回索引4

Ex: A = [ 1 2 3 9 6 4 3 10 ] find(A>9,1) would return index 4 in matlab

推荐答案

numpy中find的等效项是nonzero,但它不支持第二个参数. 但是您可以执行类似的操作来获得您想要的行为.

The equivalent of find in numpy is nonzero, but it does not support a second parameter. But you can do something like this to get the behavior you are looking for.

B = nonzero(A >= 9)[0] 

但是,如果您要寻找的是找到满足条件的第一个元素,那么最好使用max.

But if all you are looking for is finding the first element that satisfies a condition, you are better off using max.

例如,在matlab中,find(A >= 9, 1)将与[idx, B] = max(A >= 9)相同. numpy中的等效函数如下.

For example, in matlab, find(A >= 9, 1) would be the same as [idx, B] = max(A >= 9). The equivalent function in numpy would be the following.

idx = (A >= 9).argmax()

这篇关于在numpy/scipy中找到函数matlab的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆