Info

此问题已关闭。 请重新打开它进行编辑或回答。

How to get most similar row in matrix A to matrix B

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

回答(2 个)

Stephen23
Stephen23 2015-7-3
编辑:Stephen23 2015-7-13
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

Azzi Abdelmalek
Azzi Abdelmalek 2015-7-3
a = sum(abs(bsxfun(@minus,A,B)),2)
[~,t] = min(a);
out1 = A(t,:)

此问题已关闭。

标签

Community Treasure Hunt

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

Start Hunting!

Translated by