Find center of mass of parts of a matrix
23 次查看(过去 30 天)
显示 更早的评论
Hi, Let us say that I have a matrix as shown below:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/158429/image.png)
Then I apply a treshold to this matrix and am left with a region of interest. As such:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/158431/image.png)
How can I find the center of mass for this region of interest (using values from the corresponding indexes in the first matrix) and the location of this center of mass in the original matrix?
Thanks for any help
0 个评论
采纳的回答
Guillaume
2016-11-28
编辑:Guillaume
2016-11-28
Well, go back to the definition of the centre of mass, which is just a weighted mean:
%inputs:
%originalmatrix: the original matrix
%binarisedmatrix = originalmatrix > threshold; %the thresholded matrix, a logical array
[rows, cols] = ndgrid(1:size(originalmatrix, 1), 1:size(originalmatrix, 2));
rowcentre = sum(rows(binarisedmatrix) .* originalmatrix(binarisedmatrix)) / sum(originalmatrix(binarisedmatrix));
colcentre = sum(cols(binarisedmatrix) .* originalmatrix(binarisedmatrix)) / sum(originalmatrix(binarisedmatrix));
4 个评论
Guillaume
2016-11-28
Works fine for me:
originalmatrix = ones(10);
originalmatrix(4:7, 4:7) = 2 %semicolon missing for display
binarisedmatrix = originalmatrix > 1 %semicolon missing for display
[rows, cols] = ndgrid(1:size(originalmatrix, 1), 1:size(originalmatrix, 2));
rowcentre = sum(rows(binarisedmatrix) .* originalmatrix(binarisedmatrix)) / sum(originalmatrix(binarisedmatrix)) %semicolon missing for display
colcentre = sum(cols(binarisedmatrix) .* originalmatrix(binarisedmatrix)) / sum(originalmatrix(binarisedmatrix)) %semicolon missing for display
returns 5.5 for both rowcentre and colcentre
更多回答(1 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!