Dear All,
I'm trying to do the following. I have 2 very large vecotrs and two matrices. The two vecotrs are X and Y coordinates of random points. In the matrices are the x and y data points for circles aroud certain random points (from the first two vectors). To clarify a bit more: each row of a matrix is the X or Y coordinates for a circle, with i number of circles below eachother. So I'm trying to see which random points are in each of the circles.
How i do that now is with the following code:
for i=1:length(matrix(:,1))
% xcoord and ycoord are the coordinate vectors of random points, x and y are the
% coordinates of the circles
in = inpolygon(xcoord,ycoord,x(i,:),y(i,:));
% only take the indices where inpolygon is 1 and save those as cells
nz(i,:)={find(in)};
end
But this is slow progress. Is there a way to do this using matrix operations to speed it up?
With kind regards,
Tom

 채택된 답변

Matt J
Matt J 2019년 8월 26일
편집: Matt J 2019년 8월 26일

2 개 추천

I'm not sure why you are using a polygon approximation to the circles. You could just calculate the distance of all (x,y) from the centers of the circles and test whether that distance is less than the circle radius.
pdist2([X(:),Y(:)],[xc(:),yc(:)])<=radii(:).' ;

댓글 수: 3

Tom Wagemans
Tom Wagemans 2019년 8월 27일
Hi Matt,
wouldn't pdist2 work on a vector basis? As X and Y are both matrices (248345 by 100), and will be needing a forloop to do it several times? It's an interesting idea though.
kind regards,
Tom
John D'Errico
John D'Errico 2019년 8월 27일
You would essentially need loops to use inpolygon anyway. But you really don't want to use inpolygon, if these points are just circles!
One circle intersects with another circle if the Euclidean distance between the centers of the circles is less than the sum of the two radii.
So I'm not even sure if your complete approach here seems wrong. Don't work with circles ay all by creating a set of points around the circumference, but treat them properly, as circles.
Matt J
Matt J 2019년 8월 27일
편집: Matt J 2019년 8월 27일
wouldn't pdist2 work on a vector basis? As X and Y are both matrices (248345 by 100)
I don't understand the question. You told us in your initial post that X and Y are both vectors not matrices. How many circles do you have? And how many query points?
pdist2 will result in an MxN matrix where M is the number of query points and N is the number of circles. If M*N is prohibitively large, you may indeed need to break the calculation into smaller vectorized chunks, say Mx10.

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

추가 답변 (1개)

darova
darova 2019년 8월 26일

0 개 추천

Use colon (:) operator
in = inpolygon(xcoord,ycoord,x(:),y(:));

댓글 수: 4

Tom Wagemans
Tom Wagemans 2019년 8월 27일
Hi darova,
I've used the colon operator as you can see in my OP. Unfortunately this doesn't solve the need for a forloop. As X Y are matrices of 248345 by 100, in need to do inpolygon 248345 times. Each loop accesses the X Y matrix as a 1 by 100 vector. So you can imagine this taking a while.
kind regards,
Tom
darova
darova 2019년 8월 27일
SOrry, didn't unerstand the question. Matt's idea should work. Find centers, radii and use pdist2()
Sourav Mukherjee
Sourav Mukherjee 2019년 9월 28일
편집: Sourav Mukherjee 2019년 9월 28일
I am facing the same problem. Did you come up with solution? In my case, I am looking for the points within a square and not circle.
Just do something like
ind1 = x0-a/2 < x & x < x0+a/2;
ind2 = y0-b/2 < y & y < y0+b/2;
ind = ind1 & ind2;
a,b - sides of your square
x0,y0 specific point (blue points)
x,y - your data (red points)
111Untitled.png

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

카테고리

도움말 센터File Exchange에서 Logical에 대해 자세히 알아보기

제품

릴리스

R2018b

질문:

2019년 8월 26일

편집:

2020년 4월 11일

Community Treasure Hunt

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

Start Hunting!

Translated by