Reputation: 739
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]))
Upvotes: 4
Views: 1765
Reputation: 8391
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.
Upvotes: 4