Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find given row in a matrix

Tags:

matlab

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?

like image 237
shuhalo Avatar asked Jun 02 '11 02:06

shuhalo


People also ask

How do you access a row in a matrix?

To access elements in a range of rows or columns, use the colon . For example, access the elements in the first through third row and the second through fourth column of A . An alternative way to compute r is to use the keyword end to specify the second column through the last column.

How do you find the row of a matrix in Matlab?

If for example your matrix is A, you can use : size(A,1) for number of rows. size(A,2) for number of columns. Also there are some other ways like : length ( A(:,1) ) for number of rows. Sign in to answer this question.


1 Answers

EDIT:

gnovice's suggestion is even simpler than mine:

[~,indx]=ismember(X,M,'rows')  indx =       3 

FIRST SOLUTION:

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 
like image 75
abcd Avatar answered Sep 18 '22 09:09

abcd