how to locate/extract users in circle from randomly distributed many points..?

조회 수: 1 (최근 30일)
Hi everyone. I have randomly deployed users in a hexagon and circle. The problem i am facing is to locate the users which are in circle, and to find the distance of those users/points from the center of circle. Is there any way that only the users which are in circle can be extracted as shown in figure? Kindly help me with this.

채택된 답변

Image Analyst
Image Analyst 2016년 8월 28일
You must know the circle you're talking about. So let's say it is at location (xCenter, yCenter) and has radius R. I'm going to assume that when you said you "randomly deployed users in hexagon and circle" that you actually did NOT do that since you said some may not be inside the circle and you need to know which are not in the circle. So you can use sqrt() to find the distance of each user location from the center. I'll assume the users' locations are in vectors xUser and yUser. So, to find the distance of all users from the known center of the circle, you do:
distances = sqrt((xUser - xCenter) .^ 2 + (yUser - yCenter) .^ 2);
Now to find out which indexes are inside the circle, look for which distances are less than R
usersInsideCircle = distances < R;
That is a logical vector. If you want to extract the x and y of users in the circle from the entire list, use that logical index:
xInCircle = xUsers(usersInsideCircle);
yInCircle = yUsers(usersInsideCircle);
For the hexagon you might find it easier to use the inpolygon() function:
for k = 1 : length(xUsers)
usersInsideCircle(k) = inpolygon(xUsers(k), yUsers(k), xVertices, yVertices);
end
where xVertices and yVertices are the 6 vertices of your hexagon. You could use that for the circle also if you wanted to supply all the edge coordinates of the circle perimeter though there is a slight chance that a point could be inside the theoretical circle but outside the "circle" approximated by a bunch of vertex points.
  댓글 수: 8
Image Analyst
Image Analyst 2016년 10월 12일
Sorry, but no it doesn't. Just look at it in the dubugger. usersInsideCircle is a logical vector of true/1, and false/0. It is not an array of doubles with various floating point distances. You'd better check again.
FYI to get the distances themselves, you'd have to use usersInsideCircle as a logical index to distances, like this:
distancesInside = distances(usersInsideCircle);
THAT will be a vector of only the distances of users inside the circle and won't include distances of users outside the circle.
ammara khurshid
ammara khurshid 2016년 10월 12일
i was doing in wrong way. now worked. thank you.

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

추가 답변 (2개)

Walter Roberson
Walter Roberson 2016년 8월 28일
pdist2() with the second argument being the coordinates of the center of the circle. Any of the points within the cutoff radius are "in" the circle and you will know the distance.
  댓글 수: 3
Walter Roberson
Walter Roberson 2016년 8월 28일
The first argument would be the location of the points. You do know the location because you used random distribution to generate the locations.
Your figure does not appear to be attached.
Is your task to figure out which of the already-generated positions are within a particular circle, or is your task to generate positions confined to a particular circle like https://www.mathworks.com/matlabcentral/answers/294-generate-random-points-inside-a-circle

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


Image Analyst
Image Analyst 2016년 8월 28일
If you have two sets of points, red crosses and blue crosses, and want to find corresponding points, like which point in blue best matches a given point in red, this is not easy, and there may be no unique solution. There are many algorithms and you can search for "point matching algorithm" for articles: https://www.google.com/#q=point+matching+algorithm

카테고리

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