Thread Subject: Performing computation of distance Mahalanobis

Subject: Performing computation of distance Mahalanobis

From: Sprinceana

Date: 5 May, 2009 08:10:03

Message: 1 of 6

Hi everyone!

I'm trying to implement Mahalanobis distance formula in a script.

Here is my source: Mahalanobis.m

clc
clear all;
close all;


M =[1 2 3; 2 7 5; 3 5 8]; %defining my matrix

V = M(1:3:3) %transform matrix N*N to vector N*N*1

mean(V) %mean value computation for vector V
 
S=cov(M); %covariance matrix S computation
disp('Covariance matrix associated to matrix M:');
disp(S);

 T=inv(S); %inverse of the covarianc matrix
disp('inverse of the covariance matrix S');
disp(T);

%Implementing Mahalanobis formula

x=V'; %parameter for Mahalanobis formula

disp(x);


miu=mean(V'); %parameter for Mahalanobis formula

disp(miu);


%Mahalanobis general formula


distmaha=sqrt((x-miu)'*T*(x-miu));

disp(distmaha);

Documentation here:

http://en.wikipedia.org/wiki/Mahalanobis_distance

Problem:

(x-miu)'=line vector with 9 elements

T = matrix 3*3

I can't multiply (x-miu)' a vector line with 9 elements with a matrix T(3*3).

Or I have misunderstood something in computation of Mahalnobis distance.

I wait your response!

Subject: Performing computation of distance Mahalanobis

From: Sprinceana

Date: 5 May, 2009 13:26:02

Message: 2 of 6

I found that it exists in Matlab a fonction for computing directly the distance of Mahalanobis.


I discovered myself in Statistic Toolbox the Mahalanobis distance.

>>doc mahal

I want to compute the Mahalanobis distance for a big rgb image. But first I resized my image. The distance mahalanobis has 2 parameters the first is the matrix associated for my image in this case is J and the second is a parameter that performs computation of distance Mahalanobis for each line of my matrix. I don't know how tu use computation of mahal function the mathematical principe I understood and it's presented in my description.


What have I done in command window:

>> I=imread('poza1.jpg');imshow(I);
>> whos I
  Name Size Bytes Class

  I 960x1280x3 3686400 uint8 array
>>J=imresize(I, [100 100])

>> whos J
  Name Size Bytes Class

  J 100x100x3 30000 uint8 array

>>mahal(J,X)

And I want that in parameter X performs me the distance Mahalanobis for each line of my matrix so in X I want to be stored 100 values which represents the distance Mahalanobis for each line of my matrix.

I want your response with this!

I'm stuck using : >>doc mahal

Subject: Performing computation of distance Mahalanobis

From: Greg Heath

Date: 5 May, 2009 13:56:25

Message: 3 of 6

On May 5, 4:10 am, "Sprinceana " <mihai...@yahoo.com> wrote:
> Hi everyone!
>
> I'm trying to implement Mahalanobis distance formula in a script.
>
> Here is my source: Mahalanobis.m
>
> clc
> clear all;
> close all;
>
> M =[1 2 3; 2 7 5; 3 5 8]; %defining my matrix
>
> V = M(1:3:3) %transform matrix N*N to vector N*N*1
>
> mean(V) %mean value computation for vector V
>
> S=cov(M); %covariance matrix S computation
> disp('Covariance matrix associated to matrix M:');
> disp(S);
>
>  T=inv(S); %inverse of the covarianc matrix
> disp('inverse of the covariance matrix S');
> disp(T);
>
> %Implementing Mahalanobis formula
>
> x=V'; %parameter for Mahalanobis formula
>
> disp(x);
>
> miu=mean(V'); %parameter for Mahalanobis formula
>
> disp(miu);
>
> %Mahalanobis general formula
>
> distmaha=sqrt((x-miu)'*T*(x-miu));
>
> disp(distmaha);
>
> Documentation here:
>
> http://en.wikipedia.org/wiki/Mahalanobis_distance
>
> Problem:
>
> (x-miu)'=line vector with 9 elements
>
> T = matrix 3*3
>
> I can't multiply (x-miu)' a vector line with 9 elements  with a matrix =
T(3*3).
>
> Or I have misunderstood something in computation of Mahalnobis distance.
>
> I wait your response!

%X is a sample of N n-dimensional vectors

% Brute Force Technique:

X = load(data);
[N n] = size(X) % Stat Toolbox format
x0 = mean(X);
X0 = repmat(x0,N,1);
CovX = cov(X);
invC = inv(CovX);

i = 1 + floor(N*rand) % Random vector index
j = 1 + floor(N*rand)

xi = X(i,:);
xj = X(j,:);

dMi0 = (x-x0)*invC*(x-x0)'
dMij = (xi-xj)*invC*(xi-xj)'

dM = (X-X0)*invC*(X-X0)';

check1 = max(abs(dM-dM'))
check2 = max(abs(dMij - dM(i,j)))

etc.

Hope this helps.

Greg

Subject: Performing computation of distance Mahalanobis

From: Sprinceana

Date: 5 May, 2009 14:00:17

Message: 4 of 6

Any ideas please using mahal function in matlab from Statistic Toolbox?

Subject: Performing computation of distance Mahalanobis

From: Sprinceana

Date: 5 May, 2009 14:11:02

Message: 5 of 6

"Sprinceana " <mihaispr@yahoo.com> wrote in message <gtpgph$n4i$1@fred.mathworks.com>...
> Any ideas please using mahal function in matlab from Statistic Toolbox?

Thanks Greg for your idea! But I discovered a simply way to compute Mahalanobis distance using mahal function.


What have I done in command window:

>> I=imread('poza1.jpg');imshow(I);
>> whos I
  Name Size Bytes Class

  I 960x1280x3 3686400 uint8 array
>>J=imresize(I, [100 100])

>> whos J
  Name Size Bytes Class

  J 100x100x3 30000 uint8 array

>>mahal(J,X)

I don't know how to store in x my 100 values for distances for each line of the matrix(1 distance Mahalanobis is computed for each line of the matrix).

>>doc mahal



I want to store distances using mahal function for a big image that I resize like I tested in command window. I'm stuck in mahal function of the 2 parameters.

Can anyone help me please?

Subject: Performing computation of distance Mahalanobis

From: Sprinceana

Date: 5 May, 2009 20:26:02

Message: 6 of 6

Does anyone have an idea with the script the last 2lines is my problem from my code mahalanobis.m?


Also the second version to compute Mahalanobis distance is to use mahal function.

Can anyone help me please with using mahal function for a given matrix of a big image. I made the imresize of image so that it has 100*100000 pixels.

I don't understand quite well the documentation.

>>doc mahal

The thing I understood is if my matrix has 100lines for each line is calculated a Mahalanobis distance


mahal(Y,X);

Y is my given matrix.

X- is formed of Mahalanobis distance for each line of my matrix

So if I have a variable Y= 3*3 matrix I have in X 3 values of Mahalanobis distance computed for each line of my matrix.

Can anyone help me please?

Tags for this Thread

Everyone's Tags:

Add a New Tag:

Separated by commas
Ex.: root locus, bode

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Tag Activity for This Thread
Tag Applied By Date/Time
mahalanobis usi... Sprinceana 5 May, 2009 09:29:06
mahalanobis dis... Sprinceana 5 May, 2009 04:14:04
rssFeed for this Thread

Contact us at files@mathworks.com