在 numpy/scipy 中查找函数 matlab

2023-08-30Python开发问题
6

本文介绍了在 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] = max(A >= 9).numpy 中的等效函数如下.

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

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

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

The End

相关推荐

在GROUPBY集合函数中传递参数
passing parameters in groupby aggregate function(在GROUPBY集合函数中传递参数)...
2024-08-22 Python开发问题
19

通过在不与其他值冲突的情况下追加值来生成序列
Generate a sequence by appending values without clash in other values(通过在不与其他值冲突的情况下追加值来生成序列)...
2024-08-22 Python开发问题
7

pandas -按两种功能分组
pandas - Groupby two functions( pandas -按两种功能分组)...
2024-08-22 Python开发问题
2

使用Pandas GroupBy和VALUE_COUNTS查找最常用的值
Finding most common values with Pandas GroupBy and value_counts(使用Pandas GroupBy和VALUE_COUNTS查找最常用的值)...
2024-08-22 Python开发问题
12

我可以在skLearning上进行对数回归吗?
Can I make a logarithmic regression on sklearn?(我可以在skLearning上进行对数回归吗?)...
2024-08-22 Python开发问题
5

如何在Pandas中连接包含List(Series)的两列
How to concatenate two columns containing list (series) in Pandas(如何在Pandas中连接包含List(Series)的两列)...
2024-08-22 Python开发问题
2