matlab - number - Find given row in a matrix

matlab table column names (3)


gnovice's suggestion is even simpler than mine:


indx =



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


ans =


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?

Another solution that returns a row index for each occurrence of X is


Also, this solution can be easily adapted to find rows that are within threshold of X as follows (if numerical noise is an issue)

tolerance = 1e-16; %setting the desired tolerance

This is a non-loop version. It is only suitable, if M (your matrix) is not very large, ie. n and m are small. X is your row:

function ind = findRow(M,X)
    tmp = M - repmat(X,size(M,1),1); 
    ind = find(tmp,1); 

If M is too large, it might be faster, to iterate the rows of M and compare every row with your vector.

@Edit: renamed variables to match the names used in the question.