Trying to compute mahalanobis Distance without using the in built function
3 次查看(过去 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!