Trying to compute mahalanobis Distance without using the in built function

3 ビュー (過去 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 Center および File ExchangeImage Processing Toolbox についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by