필터 지우기
필터 지우기

Compute L1 distance between matrices in fast way

조회 수: 2 (최근 30일)
nedo nodo
nedo nodo 2012년 12월 20일
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
Matt J
Matt J 2013년 1월 30일
Looks fine, assuming B is a row vector.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Line Plots에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by