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

답변 (1개)

Adam
Adam 2014년 8월 20일

0 개 추천

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

Ibrahima
Ibrahima 2014년 8월 20일
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 2014년 8월 20일
편집: Adam 2014년 8월 20일
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 2014년 8월 20일
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.

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Matrices and Arrays에 대해 자세히 알아보기

태그

질문:

2014년 8월 20일

편집:

2014년 8월 20일

Community Treasure Hunt

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

Start Hunting!

Translated by