필터 지우기
필터 지우기

Count number of points around points in a list

조회 수: 6 (최근 30일)
Luis Isaac
Luis Isaac 2019년 3월 28일
답변: Luis Isaac 2019년 3월 28일
Dear;
I have two list of points defined by to array vectors xyz1 and xyz2, both of them are Nx3, where N is the number of points (different in xyz1 and xyz2) and 3 are the cartesian coordinates.
I would like to count the number of xyz2 particles in a neighborhood of each xyz1 particles, where the neighborhood is defined as a sphere of radio "h" around each element in xyz1.
I code the following:
Countxyz=zeros(size(xyz1,1),1);
h2=h*h;
for i=1:size(xyz1,1)
Dstxyz=sum((xyz2-repnat(xyz1(i,:),1)).^2,2);
Countxyz(i)=sum(Dstxyz<=h2);
end
As the number of elements in both xyz1 and xyz2 are large (more tham 10000 or 1000000) the code takes time to calculate the counts.
Is there any way to vectorize the main for loop and speed up the code?
Thanks in advance,

채택된 답변

KSSV
KSSV 2019년 3월 28일
I feel the following functions will be help ful for you. Read about knnsearch, rangesearch.

추가 답변 (1개)

Luis Isaac
Luis Isaac 2019년 3월 28일
Yes, thanks a lot
"rangesearch" is the right function to use, although must be complemented with a conversion form cell to matrix because "rangesearch" gives a cell of vectors with the indexes of the nearest neighborhood
A possible solution should be:
cellIdxNearest=rangesearch(xyz2,xyz1,h,'Distance','euclidean'); % Cell of vectors with the index of nesarest
celllenght=cellfun(@lenth,cellIdxNearest,'uni',false); % Cell of integers each of them the number of elements of each vector in de cell cellIdxNearest
Countxyz=cell2mat(celllenght); % Convert cell to matrix, in this case vector

카테고리

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

태그

Community Treasure Hunt

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

Start Hunting!

Translated by