Find all neighbors near the specified Point(x,y,z,) in 3D matrix

I want to find all neighbors who are near the specified point(x,y,z) or find all objects who are in k distance from specified point(x,y,z).
For example I want to find all objects near the point(1,1,1) or they are in k distance from point(1,1,1).
I try to use from rangesearch function, but I can't use it.

13 comentarios

Hi Erfaneh,
I guess rangesearch is for 2D not 3D, that's how it seems to me at least.
David Young
David Young el 12 de Sept. de 2014
Editada: David Young el 12 de Sept. de 2014
How are the positions of the points specified? What data structure is used, and how are x, y and z represented in it for each possible neighbour?
Do you have an N by 3 array with x in column 1, y in column 2 and z in column3? Or is it a complete N by M by Z 3D volume?
It is a complete N by M by Z 3D volume. for example I want to detect all objects near the center of 3D matrix or detect all objects that their distance are k from center of 3d matrix or any other points.
Does anybody know the solution or a hint when you have in row 1 the x values, row 2 the y values and in row 3 the z values?
Thanx
In the case of scattered points, it is not clear exactly what the question is? Perhaps you would like knnsearch()
I'm sorry I wasn't clear. I have a matrix with 3 rows e.g.
[ 1 1 1 2 2 2 3 3 3 ;
1 2 3 1 2 3 1 2 3 6 8;
1 2 3 1 2 3 1 2 3]
(my matrix is 3 x 489652 big) and the first row are the x coordinates, second the y and third the z. I have the find the coordinates (x,y,z) off all the neighbours of each point (x,y,z) so of [1;1;1], [1;2;2] and ...
Thank you
When you say "neighbours", do you mean the ones that happen to be adjacent in the matrix? SO for [1;1;1] it would have one neighbour that was [1;2;2], and for [1;2;2] it would have two neighbours that are [1;1;1] and [1;3;3] ? If so then a key question would be whether any point can be duplicated in the matrix. Either way, does order matter for the output?
If there are no duplicates, then the neighbours for YourMatrix(:,N) would always be N-1 and N+1 and there would not appear to be any point in this? At most (with no duplicates)
[sorted_coords, sort_idx] = sortrows(YourMatrix .');
and then for sorted_coords(K,:) the neighbours would be YourMatrix(:,sort_idx(K)+[-1 1])
Is the question really about plotting the edges? If so then do you have R2015b or later?
Or do you mean the 26 neighbors that a 3-d point (voxel) would have, in other words (x-1, y, z), (x+1, y, z), (x, y-1, z), (x, y+1, z), etc. until you have the coordinates of all 26 neighbors bordering that voxel?
Thank you for the responses Walter and Image Analyst. It's indead about the 26 neighbors that a 3-d point (voxel) has. However, I think I have found a possible solution without many loops. The code below gives me the indices (location) of the neighbours 26 and their distances to the point.
%
mymatrix = [ 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 3 3 3 3 3 3 3 3 3; 1 1 1 2 2 2 3 3 3 1 1 1 2 2 2 3 3 3 1 1 1 2 2 2 3 3 3; 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3]
ptCloud= pointCloud(mymatrix');
[m,n] = size(mymatrix);
for i = 1:n
point = [mymatrix(1,i) mymatrix(2,i) mymatrix(3,i)];
[indices(:,i),dists(:,i)] = findNearestNeighbors(ptCloud,point,27);
end
Unless the point cloud has special properties, the 27 nearest neighbours will include points that are not at "adjacent" coordinates. Do you have reason to expect that the points will form a cubic grid that is aligned with the axes? If so then the problem reduces to one of examining the min() and max() of the coordinates to figure out where the ranges begin and end, and determine the coordinate spacing, after which everything else is pre-determined by the properties of a cuboid with appropriate size.
You could also use meshgrid() to create a list of coordinates:
middleRow = 100; % Whatever - wherever it is for this cube.
middleCol = 1000; % Whatever - wherever it is for this cube.
middlePlane = 10; % Whatever - wherever it is for this cube.
[rows, columns, planes] = meshgrid(1:3, 1:3, 1:3)
% Make 1-D arrays
rows = rows(:) + middleRow
columns = columns(:) + middleCol
planes = planes(:) + middlePlane
% Get rid of center voxel so we have only neighbors
rows(14) = []; % Delete middle one
columns(14) = []; % Delete middle one
planes(14) = []; % Delete middle one

Iniciar sesión para comentar.

 Respuesta aceptada

Then just mask the array
withinSphere = array3D .* sphereVoxels; % Voxel values.
After that I don't know what you want because all you say is you want to "detect all objects" but "detect" is such a vague, imprecise term that I don't know what sort of numerical data to return to you. Please define "detect".

Más respuestas (0)

Etiquetas

Preguntada:

el 12 de Sept. de 2014

Comentada:

el 7 de Oct. de 2017

Community Treasure Hunt

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

Start Hunting!

Translated by