Filter löschen
Filter löschen

Finding the index of particular row in a matrix. Please explain what my code is doing?

1 Ansicht (letzte 30 Tage)
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]?

Akzeptierte Antwort

Star Strider
Star Strider am 20 Dez. 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’.

Weitere Antworten (0)

Kategorien

Mehr zu Matrix Indexing finden Sie in Help Center und File Exchange

Tags

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by