Center of mass and total mass of a matrix
173 views (last 30 days)
Show older comments
Harald Vedø Tveit
on 25 Oct 2017
Commented: Image Analyst
on 25 Jun 2021
How to calculate a center of mass and total mass of 11x4 matrix?
the matrix is as follows:
a = normrnd(451,131,11,1); b = linspace(-7.67,91.83,11)'; c = linspace(-8.3,8.3,11)'; d = linspace(0,15.21,11)';
A = [a,b,c,d]
0 Comments
Accepted Answer
Andrei Bobrov
on 25 Oct 2017
Edited: Andrei Bobrov
on 25 Oct 2017
tot_mass = sum(A(:));
[ii,jj] = ndgrid(1:size(A,1),1:size(A,2));
R = sum(ii(:).*A(:))/tot_mass;
C = sum(jj(:).*A(:))/tot_mass;
out = [tot_mass,R,C]
1 Comment
More Answers (1)
Image Analyst
on 25 Oct 2017
Two ways:
% Method 1, using mean
x = 1 : size(A, 2); % Columns.
y = 1 : size(A, 1); % Rows.
[X, Y] = meshgrid(x, y)
meanA = mean(A(:))
centerOfMassX = mean(A(:) .* X(:)) / meanA
centerOfMassY = mean(A(:) .* Y(:)) / meanA
% Method 2: using regionprops
props = regionprops(true(size(A)), A, 'WeightedCentroid')
Of course the total mass is simply sum(A(:))
totalMass = sum(A(:));
2 Comments
Sofiane Labane
on 25 Jun 2021
Edited: Image Analyst
on 25 Jun 2021
Hi. How do I calculate the center of gravity with MATLAB?
Image Analyst
on 25 Jun 2021
Center of gravity of what? What is that thing? Is it like a Guillatine blade? Or two rods hanging down? You need to explain.
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!