Info
This question is closed. Reopen it to edit or answer.
How to get most similar row in matrix A to matrix B
1 view (last 30 days)
Show older comments
Sir,I have two matrix A& B. I want to identify and get the most similar row in matrix A comparing to matrix B. I don't bother negative, positive difference, but based on absolute difference.
A=[1.2 2 3.3;1.2 2 3.2;1.1 2 3.2]
B=[1.1 2 3.2]
I used
a = sqrt(sum(bsxfun(@minus,A,B).^2));
[~,t] = min(a);
out = A(t,:);
But it gives me 2nd row as my answer, but in fact 3rd row has zero absolute difference and the 3rd row should be the similar row. Please help me how to do solve this. Many thanks in advance.
0 Comments
Answers (2)
Stephen23
on 3 Jul 2015
Edited: Stephen23
on 13 Jul 2015
By default sum sums the columns of its input matrix... whereas you want it to sum the rows instead. The second optional input argument lets us choose between these:
>> A = [1.2,2,3.3;1.2,2,3.2;1.1,2,3.2];
>> B = [1.1,2,3.2];
>> D = sqrt(sum(bsxfun(@minus,A,B).^2,2))
D =
0.1414
0.1000
0
>> [~,X] = min(D)
X =
3
>> A(X,:)
ans =
1.1000 2.0000 3.2000
0 Comments
Azzi Abdelmalek
on 3 Jul 2015
a = sum(abs(bsxfun(@minus,A,B)),2)
[~,t] = min(a);
out1 = A(t,:)
0 Comments
This question is closed.
See Also
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!