Find close points in two matrices of coordinates

I have two different length matrices of 3D coordinates. I want to find all the unique coordinates in one matrix that are within a certain Euclidean distance of any of the coordinates in the other matrix. What is the most efficient way to do this?

 Respuesta aceptada

Torsten
Torsten el 22 de Sept. de 2022
Editada: Torsten el 22 de Sept. de 2022

0 votos

Use "pdist2" to calculate the distances of the sites of the first set to the sites of the second set.
Check which rows of the distance matrix D have an entry <= dmax where dmax is your prescribed maximum distance.
These are the sites of the first set you want to determine.

3 comentarios

Adam Fitchett
Adam Fitchett el 22 de Sept. de 2022
But as i have said, the matrices are of different lengths
So I cannot directly measure the pairwise distance between elements in the two matrices
Also they are coordinates in 3D and pdist2 is only for 2D coordinates
Basically I want to know if I can do this without some complicated system of nested for loops
E.g. checking the distance between the first element of matrix1 with every element in matrix2 and then repeating that for each element of matrix1. That seems inelegant to me
Torsten
Torsten el 22 de Sept. de 2022
Editada: Torsten el 22 de Sept. de 2022
Works for me.
set1 = [0,1,4;
2 5 8];
set2 = [-1,3,6 ;...
4,2,7 ;...
-2 6 9];
dmax = 3;
distance_set1_set2 = pdist2(set1,set2)
distance_set1_set2 = 2×3
3.0000 5.0990 7.3485 4.1231 3.7417 4.2426
indices = any(distance_set1_set2 <= dmax,2)
indices = 2×1 logical array
1 0
set1(indices==1,:)
ans = 1×3
0 1 4
Adam Fitchett
Adam Fitchett el 28 de Sept. de 2022
This worked thanks, although it turned out to be very memory-hungry for my large matrices
Used 98% of the RAM on a workstation with 256 GB of RAM!

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Matrices and Arrays en Centro de ayuda y File Exchange.

Productos

Versión

R2019b

Etiquetas

Preguntada:

el 22 de Sept. de 2022

Comentada:

el 28 de Sept. de 2022

Community Treasure Hunt

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

Start Hunting!

Translated by