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

find rowsMATLAB

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]?

Best Answer

‘[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’.