justHelloWorld
justHelloWorld

Reputation: 6818

Centering matrix in matlab

I have this matlab code where: K is a square matrix matrix pXp

%center the kernel matrix
K = K - (1/p)*(K*ones(p,1))*ones(1,p) - (1/p)*ones(p,1)*(ones(1,p)*K) + (1/p^2)*sum(sum(K));

From the comment (and looking also to the description of the algorithm) this should be the process to centering K, but since the only definition of centering a matrix that I found is:

enter image description here

I don't understand the code related to the formula above. Can you please help me?

Upvotes: 1

Views: 3324

Answers (1)

ibezito
ibezito

Reputation: 5822

Explanation about the code snippet

The formula above relates to The Centering Matrix which is relevant for centering vectors (for more information, see the second part of my answer).

Your code implementation performs a centering of a matrix. There are several ways to do it, depending on what does the matrix represents.

In your case, the code performs subtraction of the matrix from the mean of each column and from the mean of each row. At the last stage, the mean value of the original matrix is being added in order to balance the final result (due to the fact that we performed two subtractions). It can be replaced by the folowing code:

K = K - repmat(mean(K,1),p,1)-repmat(mean(K,2),1,p) + mean(K(:));

For more explanation, see the following development of the formula:

enter image description here

Explanation about the Centering Matrix

The centering matrix should fulfill the following demand: when multiplying it with a vector, the result will be equal to the vector itself minus the mean (average value) of it's components.

Regarding to the equation: The first element is the unit matrix. for n=3:

In = 
 1     0     0
 0     1     0
 0     0     1

The second element in the equation is simply a matrix of ones, multiplied be 1/n. for n =3:

(1/n)*(ones(n,1)*ones(n,1)')

0.3333    0.3333    0.3333
0.3333    0.3333    0.3333
0.3333    0.3333    0.3333

Let v be a vector. In this case:

formula 1

Where the first element of this equation is simply the vector v, and the second element is a vector which contains the average value of v at each coordinate.

Upvotes: 2

Related Questions