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

1 次查看(过去 30 天)
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]?

采纳的回答

Star Strider
Star Strider 2017-12-20
‘[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’.

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

标签

产品

Community Treasure Hunt

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

Start Hunting!

Translated by