Compute L1 distance between matrices in fast way

4 次查看(过去 30 天)
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 个评论
Walter Roberson
Walter Roberson 2012-12-20
Jan, put your cursor on the text entry box without anything selected, and press the {} Code button...
Jan
Jan 2012-12-21
@Walter: Thank you very much. This is revelation.

请先登录,再进行评论。

采纳的回答

Matt J
Matt J 2012-12-20
  2 个评论
nedo nodo
nedo nodo 2013-1-30
|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

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Resizing and Reshaping Matrices 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by