Finding the index of particular row in a matrix. Please explain what my code is doing?
2 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Ruchi Bhatia
el 20 de Dic. de 2017
Comentada: Star Strider
el 20 de Dic. de 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 comentarios
Respuesta aceptada
Star Strider
el 20 de Dic. de 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 comentarios
Más respuestas (0)
Ver también
Categorías
Más información sobre Matrix Indexing en Help Center y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!