Effacer les filtres
Effacer les filtres

Compute L1 distance between matrices in fast way

2 vues (au cours des 30 derniers jours)
nedo nodo
nedo nodo le 20 Déc 2012
Hi,
I have to compute the L1 distance (Manhattan distance) between matrices. Is there a fast way that avoid to use for loop? I have found this code for euclidean distance
if true
aa=sum(a.*a,2); bb=sum(b.*b,2); ab=a*b';
d = sqrt(abs(repmat(aa,[1 size(bb,1)]) + repmat(bb',[size(aa,1) 1]) - 2*ab));
end
Thank you
  3 commentaires
Walter Roberson
Walter Roberson le 20 Déc 2012
Jan, put your cursor on the text entry box without anything selected, and press the {} Code button...
Jan
Jan le 21 Déc 2012
@Walter: Thank you very much. This is revelation.

Connectez-vous pour commenter.

Réponse acceptée

Matt J
Matt J le 20 Déc 2012
  2 commentaires
nedo nodo
nedo nodo le 30 Jan 2013
|monospacedYour solution works only if a,b have the same dimension. If a is a matrix and b is a vector how can I do?
My idea is: N=sum((abs(bsxfun(@minus,A,B)).^p),2).^(1/p);
Thank you
Matt J
Matt J le 30 Jan 2013
Looks fine, assuming B is a row vector.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Line Plots dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by