필터 지우기
필터 지우기

Find center of mass of parts of a matrix

조회 수: 23 (최근 30일)
OH
OH 2016년 11월 28일
댓글: OH 2016년 11월 29일
Hi, Let us say that I have a matrix as shown below:
Then I apply a treshold to this matrix and am left with a region of interest. As such:
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

채택된 답변

Guillaume
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
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
OH
OH 2016년 11월 29일
You are right, it works. I had just made a silly mistake. Thanks!

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

KSSV
KSSV 2016년 11월 28일
doc mean
  댓글 수: 1
OH
OH 2016년 11월 28일
Hey, thanks. I know how to get the mean value of the region of interest: let us call the first matrix A and the tresholded matrix, where the region of interest indexes = 1 and outside region indexes = 0,B. Then I find the mean value by: mean(A(B==1))
However, I need to find the location of the center of mass in A

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by