필터 지우기
필터 지우기

Trying to compute mahalanobis Distance without using the in built function

조회 수: 11 (최근 30일)
Lora
Lora 2014년 3월 31일
댓글: Youssef Khmou 2014년 3월 31일
Hello I am trying to wrtite a function where i am trying to compute mahalanobis Distance. I am stuck as it throws an error that dimensions of the Matrices do not match now i know the error is clear but i am trying to follow the equation defination Is x in this equation represents pixel value at each row and column? If yes then where am i missing the trick if any one can guide me Thanks anyways guys
if true
% code
function imDistance = mahalanobisDistance(im, modelMean, modelSigmaInv)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
for x=size(im,1)
for y=size(im,2)
a=im(x,y)-modelMean;
end
end
b=a.*modelSigmaInv;
c=b.*a;
imDistance=sqrt(c);
end

답변 (1개)

Youssef  Khmou
Youssef Khmou 2014년 3월 31일
Lora, The build in function is in square unite , but here is an example on how to write a function :
x=randn(4,100);
R=cov(x');
for n=1:100
d(n)=sqrt(x(:,n)'*inv(R)*x(:,n));
end
plot(d.^2)
hold on
plot(mahal(x',x'),'r')
  댓글 수: 3
Lora
Lora 2014년 3월 31일
Well thanks a lot for the help but i wrote it in description as wekk that i cannot use Mat lab in built function Mahal.
Youssef  Khmou
Youssef Khmou 2014년 3월 31일
try to study the following function :
function d=Mahald(x,y)
% this a simple version : x and y must have the sime size, rows represent
% the dimensions, columns the observations.
[K,N]=size(x);
N=length(x); % length(y)
R=(x*x')/N;
d=zeros(1,N);
for n=1:N
d(n)=sqrt(x(:,n)'*inv(R)*y(:,n));
end
% More enhancements can be made for arbitrary sizes of x,y .

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

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by