0

How can I find which row in a matrix has a specified set values that I entered in an array?

So for example;

A = [4 5 6 7; 
     8 4 5 6;
     4 5 6 8;
     8 4 8 9;
     1 2 2 4;
     5 3 4 6];

and I want to find which row has the vector of [4 5 6 8]

0

2 Answers 2

1

You can use a combination of all and find...

With implicit expansion (R2016b or newer)

find( all( A == [4 5 6 8], 2 ) )

Equivalently you can use bsxfun (compatible with all MATLAB versions)

find( all( bsxfun( @eq, A, [4 5 6 8] ), 2 ) )

The output in both cases is 3 from your example A.

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

Comments

0

I would go for @Wolfie's approach, assuming order matters.

Another possibility is to use ismember. This can be used both when order matters and when it doesn't. Let

A = [4 5 6 7; 8 4 5 6; 4 5 6 8; 8 4 8 9; 1 2 2 4; 5 3 4 6];
v = [4 5 6 8];
  • If order matters:

    result = find(ismember(A, v, 'rows'));
    
  • If order doesn't matter:

    result = find(all(ismember(A, v), 2));
    

Comments

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.