Counting spheres within a certain radius of a sphere in a dense packing
Show older comments
I have a large set of X,Y,Z coordinates which define the centre points of my spheres in a certain packing. I want to be able to count the number of spheres within a particular distance of each sphere, i.e. how many sphere's are within 1R or 2R of each sphere.
At the moment I'm just checking which other coordinates are nearby using an if loop to check if delta x, y and z are less than 1R. If all 3 deltas are within than value then I count the point.
However as I do this with a large data set I want to try and see if there is a better way of doing this.
Any Suggestions?
JP
2 Comments
Walter Roberson
on 8 Dec 2011
There is no such thing as an if loop.
Image Analyst
on 8 Dec 2011
No sphere will be within 1R or 2R of another unless they can penetrate each other, or unless you're talking about outer surface to center instead of center to center, or unless that R is not the R of the spheres but something greater than twice the sphere radius.
By the way, an interesting book on spatial statistics is here: http://www.csiro.au/resources/~/media/CSIROau/Divisions/CSIRO%20Mathematics%20Informatics%20%20Statistics/Rspatialcourse_CMIS_PDF%20Standard.pdf
Accepted Answer
More Answers (2)
Walter Roberson
on 22 Aug 2011
0 votes
A close relative of this question came up several times last fall (almost as if it had been assigned as a class project at the time!). The question then had to do with building simulators for soil deposition, through either water or wind action. As the deposition was random, the placement of new particles depended upon the ability to detect that there were no existing particles at the proposed new site.
Unfortunately in those series of posts, no-one had a proposal that was faster than basic trial-and-error. Except that I proposed that octrees could make that particular task more efficient.
I have kept the problem simmering in the back of my mind since, hoping that some day I might come up with a nifty mathematical transformation that would make it much faster, but unfortunately I have not come up with one.
In your current question, are you doing this probing for a small number of points, or for all (or the majority of) points? It might not be out of the question that a "sliding window" style approach might help.
Alternately, perhaps a vectorized calculation of the distance from every sphere to every other might work better than one might expect, as it could be largely BLAS'd and you could skip the sqrt() step. repmat() the squares of the individual sphere radii (if the 1R and 2R vary per sphere), sum(A < B), subtract 1 because the self-distance is 0... might turn out to be efficient enough, perhaps much more efficient than trying to be "smart" about it.
Image Analyst
on 9 Dec 2011
0 votes
The stats program R has a plug-in called spatstat. spatstat has a function called pairdist that calculates the distances. There are a bunch of other types of metrics it also uses to characterize the spatial statistics. Maybe if you get that package it might have the source code. See chapter 19 of this book: http://www.csiro.au/resources/~/media/CSIROau/Divisions/CSIRO%20Mathematics%20Informatics%20%20Statistics/Rspatialcourse_CMIS_PDF%20Standard.pdf Even if you can't get source code, this book is an excellent resource for all kinds of ways to analyze spatial distributions for "regularity," spatial uniformity, clumping, etc.
Categories
Find more on Surface and Mesh Plots in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!