I have an m by n matrix in MATLAB
, say M. I have an n-element row vector, i.e. a one by n column matrix, say X.
I know X is a row somewhere in M. How can I find the index in M?
Question&Answers:osI have an m by n matrix in MATLAB
, say M. I have an n-element row vector, i.e. a one by n column matrix, say X.
I know X is a row somewhere in M. How can I find the index in M?
Question&Answers:osgnovice's suggestion is even simpler than mine:
[~,indx]=ismember(X,M,'rows')
indx =
3
You can easily do it using find
and ismember
. Here's an example:
M=magic(4); %#your matrix
M =
16 2 3 13
5 11 10 8
9 7 6 12
4 14 15 1
X=[9 7 6 12]; %#your row vector
find(ismember(M,X),1)
ans =
3