Effacer les filtres
Effacer les filtres

Trying to compute mahalanobis Distance without using the in built function

11 vues (au cours des 30 derniers jours)
Lora
Lora le 31 Mar 2014
Commenté : Youssef Khmou le 31 Mar 2014
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

Réponses (1)

Youssef  Khmou
Youssef Khmou le 31 Mar 2014
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 commentaires
Lora
Lora le 31 Mar 2014
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 le 31 Mar 2014
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 .

Connectez-vous pour commenter.

Catégories

En savoir plus sur Programming dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by