large array distance calculation

Hi everyone, I have a large array "M" (9000000x4 double) which 3 first columns are spatial coordinates x,y and z. The last column of the array specifies if a coordinate value is 0 or 1.
For each "1" coordinate, I would like to calculate the minimum distance to a "0" coordinate among all "0" coordinates, using Euclidean distance 3D formula.
Since the array is very large I'm trying to do it without any loop but I can't figure how to do it efficiently.
Thanks in advance for your support. Damien

Respuestas (1)

Adam
Adam el 20 de Ag. de 2014
i = 3;
min( sqrt( sum( bsxfun( @minus, M(i,1:3), M(M(:,4) == 0,1:3) ).^2, 2 ) ) );
I think that could be part of a solution in that for a given 1 coordinate (e.g. row 3) it will calculate the minimum distance.
To wrap that up for all 1 coordinates would require a little more thought though.
If I have time and inspiration I might be able to supply that or someone else can extend that or provide a totally different full solution!

3 comentarios

Ibrahima
Ibrahima el 20 de Ag. de 2014
Thanks for your answer Adam, I didn't think of that way to calculate the minimum distance for a given 1 coordinate.
Though the actual barrier I am facing is how to do it without the "i" loop...
Adam
Adam el 20 de Ag. de 2014
Editada: Adam el 20 de Ag. de 2014
I think...
f = @(x) min( sqrt( sum( bsxfun( @minus, x(1:3), M(M(:,4) == 0,1:3) ).^2, 2 ) ) )
D = rowfun( f, table( M ) )
would give you an answer for all rows but would obviously be 0 in all cases where you have a 0-coordinate anyway.
I only created a small 5*4 matrix with two 1-coordinates and two 0-coordinates though to test that.
It would also only work if you have Matlab 2014 where table was introduced.
I suspect from a speed perspective it will also be slower than the for loop even though it replaces the need for one. Functions like arrayfun, rowfun etc tend not to be worthwhile for pure speed.
Adam
Adam el 20 de Ag. de 2014
Yes, a quick test on this (only a 20 by 4 matrix though) shows that this approach is slower than just using a for loop as I suspected.
There may be an approach that uses neither a for loop nor one of the "hidden for loop" approaches, but I can't think of it off-hand.

Iniciar sesión para comentar.

Categorías

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

Etiquetas

Preguntada:

el 20 de Ag. de 2014

Editada:

el 20 de Ag. de 2014

Community Treasure Hunt

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

Start Hunting!

Translated by