필터 지우기
필터 지우기

summation of a matrix inside circle

조회 수: 10 (최근 30일)
JK
JK 2021년 11월 10일
편집: DGM 2021년 11월 11일
Hello,
I have 2D NxN matrix array and I want to get summation of data inside circle from center.
The size of circle is varied from smallest i.e. one pixel at the center to largest i.e. N/2
I want to plot summation w.r.t. circle.
Thanks,
  댓글 수: 1
DGM
DGM 2021년 11월 10일
Is N always odd? Is it always even? Could it be either?

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

채택된 답변

DGM
DGM 2021년 11월 10일
편집: DGM 2021년 11월 11일
This should work regardless of whether N is odd or even.
s = 100;
A = ones(s); % test array
% build distance map
rmax = s/2 - 0.5;
x = linspace(-rmax,rmax,s);
D = sqrt(x.^2 + x.'.^2);
r = 0:ceil(D(1,floor(s/2)));
circint = zeros(1,numel(r));
for rind = 1:numel(r)
circint(rind) = sum(A(D<=r(rind)));
end
plot(r,circint)
  댓글 수: 2
JK
JK 2021년 11월 11일
any thought on even case?
DGM
DGM 2021년 11월 11일
편집: DGM 2021년 11월 11일
I modified it. Bear in mind that there is no center pixel in an array with even geometry. The center four pixels are 1/sqrt(2) away from the center. Consequently, the first element of the output is zero.
Similarly, the pixels straddling the midpoints of the edges of a 10x10 array are not at a radius of 5px. They're at a radius of 4.5227px. As the radius vector in this example is integer-valued, you can either choose to stop short at 4px or go a bit too far (5px). I chose to go to 5px. If you want to stop short in the even case, you can remove the ceil() from the definition of r.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by