将MATLAB中的find()转换为python [英] Converting find() in matlab to python

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

问题描述

我正在将代码从Matlab转换为Python.Matlab中的代码是:

I am converting a code from Matlab to Python. The code in Matlab is:

x = find(sEdgepoints > 0 & sNorm < lowT);
sEdgepoints(x)=0;

两个数组的大小相同,我基本上是在创建蒙版.

Both arrays are of same size, and I am basically creating a mask.

我阅读了这里 numpy中的nonzero()等同于find(),因此我使用了它.在Python中,我将dstc用于sEdgepoints,将dst用于sNorm.我也直接输入了lowT =60.所以,代码是

I read here that nonzero() in numpy is equivalent to find(), so I used that. In Python, I have dstc for sEdgepoints and dst for sNorm. I have also directly put in lowT = 60. So, the code was

x = np.nonzero(dstc > 0 and dst < 60)
dstc[x] = 0

但是,我收到以下错误消息:

But, I get following error:

Traceback (most recent call last):
File "C:\Python27\Sheet Counter\customsobel.py", line 32, in <module>
    x = np.nonzero(dstc > 0 and dst < 60)
ValueError: The truth value of an array with more than one element is 
ambiguous. Use a.any() or a.all()

我阅读了有关a.any()/a.all()的用法在这篇文章中,我不确定该如何运作.因此,我有两个问题:1.如果可以,使用哪个数组?2.如果我是正确的但它不起作用,如何转换代码?

I read about the usage of a.any()/a.all() in this post, and I am not sure how that will work. So, I have two questions: 1. If it does, which array to use? 2. If I am correct and it does not work, how to convert the code?

推荐答案

进行布尔运算,而numpy希望您进行按位运算,因此必须使用&

and does boolean operation and numpy expects you to do bitwise operation, so you have to use & i.e

x = np.nonzero((dstc > 0) & ( dst < 60))

这篇关于将MATLAB中的find()转换为python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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