Info

Diese Frage ist geschlossen. Öffnen Sie sie erneut, um sie zu bearbeiten oder zu beantworten.

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

2 Ansichten (letzte 30 Tage)
Mekala balaji
Mekala balaji am 3 Jul. 2015
Geschlossen: MATLAB Answer Bot am 20 Aug. 2021
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.

Antworten (2)

Stephen23
Stephen23 am 3 Jul. 2015
Bearbeitet: Stephen23 am 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

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

Diese Frage ist geschlossen.

Community Treasure Hunt

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

Start Hunting!

Translated by