How can I calculate the number of data points within the area of a cirle
조회 수: 26 (최근 30일)
이전 댓글 표시
Basically I need to select a data point within a certain range (which I cant do) then calculate the number of data points within the area of a circle of specific radius (which i have absolutely no idea how to do)
if someone can help me out it would be much appreciated
댓글 수: 1
Star Strider
2012년 10월 2일
You need to be a bit more specific. Assuming that your data are (x,y) pairs:
What is the nature of the data that makes it impossible for you to select data within a certain range? What is the range and what are the data?
Do you know the center of the circle as well as its radius?
답변 (5개)
Star Strider
2012년 10월 2일
편집: Star Strider
2012년 10월 3일
I'm not claiming this is the most elegant or most efficient way to do it, but here's my solution:
% Generate data ...
XY = (0.5-rand(45,2))*50;
% Select ranges ...
xrange = [-20 20];
yrange = [ -5 45];
% Find data within original ranges ...
xidx = find( (XY(:,1) >= xrange(1)) & (XY(:,1) <= xrange(2)) );
yidx = find( (XY(:,2) >= yrange(1)) & (XY(:,2) <= yrange(2)) );
didx = intersect(xidx,yidx);
% Select a point ...
sampleidx = randi([1 length(didx)]);
% Determine a center & radius ...
circtr = XY(didx(sampleidx),:);
cirrad = 15;
% Calculate radii of all points from center ...
radii = hypot( (XY(:,1)-circtr(1)), (XY(:,2)-circtr(2)) );
% Find the radii <= defined radius
incirc = find( radii <= cirrad);
excirc = setdiff([1:size(XY,1)],incirc);
% Generate table of data within radius ...
data = XY(incirc,:);
% Calculate circle for plot ...
cirx = [(circtr(1)-cirrad) : 0.1 : (circtr(1)+cirrad)];
cirplt = [circtr(1)+cirrad*cos([0:0.025:1]'*2*pi) circtr(2)+cirrad*sin([0:0.025:1]'*2*pi)];
figure(1)
plot( cirplt(:,1), cirplt(:,2), '-b')
hold on
plot( XY(incirc,1), XY(incirc,2), '.r')
plot( XY(excirc,1), XY(excirc,2), '.g')
hold off
axis equal
grid
The number of data within the circle is length(incirc) if that information is also important.
댓글 수: 0
Image Analyst
2012년 10월 2일
You can use the first code sample in the FAQ: http://matlab.wikia.com/wiki/FAQ#How_do_I_create_a_circle.3F Just sum circlePixels to sum up the pixels inside the circle.
댓글 수: 0
Babak
2012년 10월 2일
data = [25 4 -10 -8 20 -5 -15 2 12 22 1000];
range = [-10 10];
% data-range(1)>0
% data-range(2)<0
sum((data-range(1)>0) & (data-range(2)<0))
% gives you the number of elements in data that are in the range
for the case of a circle and the elements in a circle you can use the same strategy but in polar coordinates
댓글 수: 0
Babak
2012년 10월 2일
Here is how you do it for numbers in a circle:
x = [6 -5 -2 3 -1 0 2 1 8 -12 7 6 -6 5 -7 41 3 ];
y = [3 2 -4 -4 -1 77 8 -6 3 -3 3 -9 -12 8 -7 2 -9 ];
center_of_circle=[3 -5];
radius=6;
x0 = x - center_of_circle(1);
y0 = y - center_of_circle(2);
sum(x0.^2+y0.^2-radius^2<0) % gives you the number of elements in the circle
댓글 수: 0
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!