How can I check if row matrix B is equal to a row of matrix A?

32 vues (au cours des 30 derniers jours)
laurie
laurie le 9 Mar 2015
Commenté : Star Strider le 9 Mar 2015
I have a matrix A = [1 2 3; 4 5 6; 7 8 9]
I randomly generate matrix B = [4 5 6]
How can I check if B is a row of A in ONE loop or using NO loops?

Réponse acceptée

Star Strider
Star Strider le 9 Mar 2015
Look Laurie! NO LOOPS!
A = [1 2 3; 4 5 6; 7 8 9];
B = [4 5 6];
[Result,LocResult] = ismember(B,A,'rows')
produces:
Result =
1
LocResult =
2
The ‘Result’ output indicates that there is a match, and ‘LocResult’ that the match is the second row.
  2 commentaires
laurie
laurie le 9 Mar 2015
wow that's so cool. Thanks!
Star Strider
Star Strider le 9 Mar 2015
My pleasure!
Have fun with MATLAB!

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by