替换python中的matlab find函数 [英] Replacement of matlab find function in python

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

问题描述

我正在尝试找到合适的 python 函数来替换脚本中的 matlab find 并通过一些谷歌搜索我看到 np.where() 解决了大部分目的时间.但是在双重条件的情况下,我有不同的输出.有人可以告诉我这种方法有什么问题以及如何继续吗?示例代码和区别如下.

I am trying to find suitable python function to replace matlab find in my script and with some google search I see np.where() solves the purpose most of the time. But in case of double condition, I have different outputs. Can someone tell me whats wrong with this approach and how to go ahead? The example code and difference is below.

在 matlab 的情况下:

In case of matlab:

b = [1, 2, 3; 1, 2, 3; 1, 2, 3]
[I, J] = find(( b > 1) & (b <= 3))

给出输出

I =                 J = 
     1                 2
     2                 2
     3                 2
     1                 3
     2                 3
     3                 3

如果是python:

b= array([[1, 2, 3],
       [1, 2, 3],
       [1, 2, 3]])

>>> np.where((b > 1) & (b <= 3))
(array([0, 0, 1, 1, 2, 2]), array([1, 2, 1, 2, 1, 2]))

推荐答案

虽然顺序和索引条件不同,但两种方法确实提供了相同的答案.

Both methods do provide the same answer, although order and indexing conditions are different.

Python 的数组索引从 0 开始,就像在 C 中一样,而 matlab 的索引从 1 开始.

Python indexing of arrays starts from 0, like in C, whilst matlab's one starts from 1.

此外,两个输出(通过 matlab 和 numpy)确实彼此对应项的排列.这可能是由于不同的索引实现.

Moreover, the two outputs (by matlab and numpy) do correspond one another modulo a permutation of the terms. Likely this is due to different indexing implementations.

您可以看到 matlab 按列遍历矩阵,而 numpy 按行遍历.

You can see that matlab goes through your matrix by columns, while numpy by rows.

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

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