Finding the index of particular row in a matrix. Please explain what my code is doing?
3 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Ruchi Bhatia
le 20 Déc 2017
Commenté : Star Strider
le 20 Déc 2017
Hello,
If I have a matrix
x y
A=[ 2.5 3.5
1.2 3.5
2.2 1.2
1.2 3.5
4.0 2.2 ]
B = [2.2 1.2]
I need to understand what the following two lines of codes are doing?
[x,y]=find(A(:,1)==B) returns x=3,2,4 and y=1,2,2 --- is this searching only in first column for 2.2? what does x and y mean in this case?
[x,y]=find(A==B) returns x=3,3 and y=1,2 -- is this searching the entire row for [2.2,1.2]?
0 commentaires
Réponse acceptée
Star Strider
le 20 Déc 2017
‘[x,y]=find(A(:,1)==B) returns x=3,2,4 and y=1,2,2 --- is this searching only in first column for 2.2? what does x and y mean in this case?’
Here, ‘x’ and ‘y’ are the row and column indices respectively. The line of code you quote in this line looks at the first column of ‘A’ for any values that match either of the values in ‘B’. Those values are referred to in the row and column indices respectively, specifically (3,1), (2,2), and (4,2).
‘[x,y]=find(A==B) returns x=3,3 and y=1,2 -- is this searching the entire row for [2.2,1.2]?’
Yes. It is matching the rows. The (x,y) indices are (3,1) and (3,2), so it is telling you that all of row 3 of ‘A’ matches ‘B’.
2 commentaires
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Matrix Indexing dans Help Center et File Exchange
Produits
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!