Hello Phil,

This should work:

--> [A,B] = ndgrid(rand(1,1000), rand(1,10000));  // test's data
--> size(A)
 ans  =
   1000.   10000.
--> [v, i] = min(abs(A-B));

The minimal distances are in v, the index of the closest in B are in i.

Regards
Samuel

Le 15/02/2019 à 21:46, P M a écrit :
Hi experts,

my problem is (I guess) a very common thing, but I somehow get stucked.

I aim to find the nearest position of ANY value in array_1 to ANY position in array_2.

e.g.:

A = array of doubles with 1000 elements.
B = array of doubles with 10'000 elements.

for each element of A I want to know it's nearest value in B.
Also I want to know the index of the nearest value in B.

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.

something like this:

nearesVal   = zeros(1000);
distIndex    = zeros(1000);

for i=1:1000
d = 1000000; // just a rediculous big number that is much greater than max(A) & max(B)
  for j = 1:10000
     act_dist = abs( A(i) - B(j) );     // actual minimal distance
     if act_dist <= d
        d = act_dist;
        nearestVal(i) = B(j);
        distIndex(i)  = j;
      end
   end
end

I do not have Scilab at hand, while I am writing this, but it looks like it should work :-)

Thanks
Phil










_______________________________________________
users mailing list
[email protected]
http://lists.scilab.org/mailman/listinfo/users


_______________________________________________
users mailing list
[email protected]
http://lists.scilab.org/mailman/listinfo/users

Reply via email to