Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to calculate the centroid of a matrix?

I have the following 5x5 Matrix A:

1 0 0 0 0 
1 1 1 0 0 
1 0 1 0 1 
0 0 1 1 1 
0 0 0 0 1

I am trying to find the centroid in MATLAB so I can find the scatter matrix with:

Scatter = A*Centroid*A'
like image 597
user1077071 Avatar asked Nov 18 '25 05:11

user1077071


1 Answers

If you by centroid mean the "center of mass" for the matrix, you need to account for the placement each '1' has in your matrix. I have done this below by using the meshgrid function:

M =[    1 0 0 0 0; 
        1 1 1 0 0; 
        1 0 1 0 1; 
        0 0 1 1 1; 
        0 0 0 0 1];

[rows cols] = size(M);

y = 1:rows;
x = 1:cols;

[X Y] = meshgrid(x,y);

cY = mean(Y(M==1))
cX = mean(X(M==1))

Produces cX=3 and cY=3;

For

M = [1 0 0;
     0 0 0;
     0 0 1];

the result is cX=2;cY=2, as expected.

like image 131
Vidar Avatar answered Nov 19 '25 23:11

Vidar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!