4

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.

In case of matlab:

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

Gives output

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

In case of 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]))

1 Answer 1

4

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

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

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.

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

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. If I transpose the matrix to perform the operation in Python I get the same answer as by Matlab. Just that it returns J as I now and I as J, but that is OK.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.