Find closest matrix from list

I've got a matrix A(3x4) and a list of similar matrix M(i).x, where i=1:100. I need to find the matrix from list M which will be closest to my matrix A. How can I do that?

3 commentaires

Azzi Abdelmalek
Azzi Abdelmalek le 30 Juil 2013
What is your criterion ? What should we compare to tell that a is closer then b to c?
Iain
Iain le 30 Juil 2013
closest in what sense?
if:
A = [0 1];
which would be closer
M(1).x = [0 1000];
M(2).x = [500 501];
M(3).x = [200 -200];
Jan
Jan le 30 Juil 2013
Modifié(e) : Jan le 30 Juil 2013
@lain: On first sight M(1).x is closest, because it is found in the topmost line. But if A is defined after M, M(3).x is closest. ;-)

Connectez-vous pour commenter.

Réponses (3)

Jan
Jan le 30 Juil 2013

0 votes

bestValue = -Inf;
bestIndex = 0;
for k = 1:numel(M)
Value = getDistance(A, M(k).x);
if Value > bestValue
bestValue = Value;
bestIndex = k;
end
end
Now insert your criterion to determine the distance as you like. Perhaps you are looking for "Value < bestValue" and want to start with +Inf.

1 commentaire

Andrew
Andrew le 30 Juil 2013
Sorry. but I can't find function getDistance in a Matlab Help

Connectez-vous pour commenter.

Azzi Abdelmalek
Azzi Abdelmalek le 30 Juil 2013
Modifié(e) : Azzi Abdelmalek le 30 Juil 2013

0 votes

I propose this criterion sum(abs(a-b)) to be the smallest
for k=1:100
s(k)=sum(sum(abs(A-M(k).x)))
end
[~,idx]=min(s);
Res=M(idx).m
Andrew
Andrew le 30 Juil 2013
Modifié(e) : Andrew le 30 Juil 2013

0 votes

Sorry for incomplete question.
For example I've got matrix A[ 1 2 2; 1 2 3; 1 2 4] and in a list is present matrix M(3).x=[ 1 2 3; 1 2 3; 1 2 4] and M(4).x=[ 1 2 4; 1 2 3; 1 2 4]. Than matrix M(3).x will be closest. A can't use mean or sum of values in matrix to compare.

6 commentaires

Azzi Abdelmalek
Azzi Abdelmalek le 30 Juil 2013
Modifié(e) : Azzi Abdelmalek le 30 Juil 2013
Why M(3) is the closest? Are you comparing just by eyes?
Andrew
Andrew le 30 Juil 2013
Because the difference is only 1 with M(3) and 2 with M(4).
A[ 1 2 2; 1 2 3; 1 2 4]
M(3).x=[ 1 2 3; 1 2 3; 1 2 4]
M(4).x=[ 1 2 4; 1 2 3; 1 2 4]
What about this case
M(3).x=[ 5 22 3; 10 21 3; 1 52 4]
M(4).x=[ 1 2 44; 11 2 13; 15 2 4]
Andrew
Andrew le 30 Juil 2013
Modifié(e) : Andrew le 30 Juil 2013
yes, here is a problem. I have to find the closest value, but there should be some level of difference. For example, if the mean value of the first 3 numbers in M(3) differ from the first three values in A for 10, second three values for 5 and the third one for 5, and in M(4) differs for 7,7,7 respectively, it better to choose M(4) because the mean difference of each value is less. Somehow in that way. Sorry for English, I hope you understand what I mean.
Iain
Iain le 30 Juil 2013
The question is what definition are you using for "distance" all these are valid options...
distance = max(abs(A(:)-M(i).x(:)));
distance = sum(abs(A(:)-M(i).x(:)));
distance = sum((A(:)-M(i).x(:)).^2);
distance = max(abs(A(:)-M(i).x(:)+mean(M(i).x(:))-mean(A(:)) ));
Andrew
Andrew le 30 Juil 2013
Oh, I've just realized that I can use
distance = sum(abs(A(:)-M(i).x(:)));
to find the closest and use
distance = max(abs(A(:)-M(i).x(:)));
to cut off that values which are too "far". Okay, thanks a lot for your help!! I will try to do that.

Connectez-vous pour commenter.

Catégories

Produits

Question posée :

le 30 Juil 2013

Community Treasure Hunt

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

Start Hunting!

Translated by