Find closest matrix from list
Mostrar comentarios más antiguos
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 comentarios
Azzi Abdelmalek
el 30 de Jul. de 2013
What is your criterion ? What should we compare to tell that a is closer then b to c?
Iain
el 30 de Jul. de 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];
Respuestas (3)
Jan
el 30 de Jul. de 2013
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 comentario
Andrew
el 30 de Jul. de 2013
Azzi Abdelmalek
el 30 de Jul. de 2013
Editada: Azzi Abdelmalek
el 30 de Jul. de 2013
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
6 comentarios
Azzi Abdelmalek
el 30 de Jul. de 2013
Editada: Azzi Abdelmalek
el 30 de Jul. de 2013
Why M(3) is the closest? Are you comparing just by eyes?
Andrew
el 30 de Jul. de 2013
Azzi Abdelmalek
el 30 de Jul. de 2013
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]
Iain
el 30 de Jul. de 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
el 30 de Jul. de 2013
Categorías
Más información sobre Matrix Indexing en Centro de ayuda y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!