Skip to content Skip to sidebar Skip to footer

Converting Find() In Matlab To Python

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 b

Solution 1:

Try np.argwhere() (and note the importance of the () around the inequalities):

>>X=np.array([1,2,3,4,5])
>>Y=np.array([7,6,5,4,3])
>>ans = np.argwhere((X>3) & (Y<7))
>>ans 

array([[3],
   [4]])

Solution 2:

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))

Solution 3:

You could implement it yourself like:

x = [[i,j] for i, j in zip(sEdgepoints , sNorm ) if i > 0 and j < lowT]

Will give you a list of of lists corresponding to your the matching constraints. I guess this might not be exactly what you are looking for.

Maybe look at the pandas module, it makes masking more comfortable than plain python or numpy: https://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.mask.html

Post a Comment for "Converting Find() In Matlab To Python"