Trying to compute mahalanobis Distance without using the in built function
조회 수: 1 (최근 30일)
이전 댓글 표시
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
댓글 수: 0
답변 (1개)
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
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 Center 및 File Exchange에서 Image Processing Toolbox에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!