<div dir="ltr"><div>Hi experts,</div><div><br></div><div>my problem is (I guess) a very common thing, but I somehow get stucked.</div><div><br></div><div>I aim to find the nearest position of ANY value in array_1 to ANY position in array_2.</div><div><br></div><div>e.g.:</div><div><br></div><div>A = array of doubles with 1000 elements.</div><div>B = array of doubles with 10'000 elements.</div><div><br></div><div>for each element of A I want to know it's nearest value in B.</div><div>Also I want to know the index of the nearest value in B.<br></div><div><br></div><div>I could do it in a bunch of for-loops, but this is way to slow, since this loops 1000 values against 10'000 values of B.<br></div><div><br></div><div>something like this:</div><div><br></div><div>nearesVal   = zeros(1000);<br></div><div>distIndex    = zeros(1000);<br></div><div><br></div><div>for i=1:1000</div><div>  d = 1000000; // just a rediculous big number that is much greater than max(A) & max(B)

</div><div>  for j = 1:10000</div><div>     act_dist = abs( A(i) - B(j) );     // actual minimal distance   <br></div><div>     if act_dist <= d</div><div>        d = act_dist;</div><div>        nearestVal(i) = B(j);

</div><div>        distIndex(i)  = j;</div><div>      end</div><div>   end</div><div>end<br></div><div><br></div><div>I do not have Scilab at hand, while I am writing this, but it looks like it should work :-)</div><div><br></div><div>Thanks</div><div>Phil<br></div><div><br></div><div><br></div><div><br></div><div><br></div><div><br></div><div><br></div><div><br></div><div><br></div></div>