# Storing neighboring coordinates within a sphere from a 3D domain.

2 views (last 30 days)
Samuel Thompson on 5 Oct 2017
Commented: Cedric Wannaz on 5 Oct 2017
Hi,
I wish to be able to form connections between nodes that are within a specified distance apart in 3D space.
~
So far I have created a script that generates coordinates for random nodes within a 3D domain. The coordinates are stored in an N x 3 array, where (:,1) refers to the x axis, (:,2) refers to the y axis, (:,3) refers to the z axis and N refers to the number of nodes.
I wish to take node 40 for example (see below), and identify all of the other nodes that are within a sphere (of radius r) that surrounds it. It can be seen that, to name a few, nodes 5, 7, 10, 15 and 51 lie within this region.
My ideal output would be an array with 2 columns that stores the node number of each of these nodes. For the given example, the desired output would be X = [40, 5; 40, 7; 40, 10; 40, 15; 40, 51 ...]
Any help would be appreciated, please find the code attached.
Sam
I have generated the sphere using the method suggested by 'Image Analyst'.

Cedric Wannaz on 5 Oct 2017
Edited: Cedric Wannaz on 5 Oct 2017
Use PDIST2 and get points within a distance smaller (or equal) to the center (node) than the radius.
EDIT : here is a quick example, where we have 4 nodes and we are interested in building a cell array of nodes (IDs) that are within in radius 2 of two centers:
>> dNodes2Centers = pdist2( [1,0,0; 0,2,0; 0,0,3; 1,1,0], [0,0,0; 0,1,0] )
dNodes2Centers =
1.0000 1.4142
2.0000 1.0000
3.0000 3.1623
1.4142 1.0000
>> [r, c] = find( dNodes2Centers < 2 ) ; % R = 2, exclude boundary (<).
>> nodeIdsPerCenter = accumarray( c, r, [], @(x){x} ) ; % Group per center.
which builds:
>> celldisp( nodeIdsPerCenter )
nodeIdsPerCenter{1} =
1
4
nodeIdsPerCenter{2} =
1
2
4
##### 2 CommentsShowHide 1 older comment
Cedric Wannaz on 5 Oct 2017
My pleasure!