A simpler way --

outarr=zeros(1000)
for i=1:1000
[m,k]=min(abs(B-A(i));
outarr(i)=k;
end;
This will produce a list in outarr such that outarr(n) is the index of the
element in B closest to the n'th element in A .

If you want to go faster, consider setting up  a binary tree containing B.
Then use the tree to search for nearest neighbor to A(i) and to -A(i).
Then decide which neighbor is closer.  (I think this works).
Searching the tree goes as log of length of B at the expense of a linear
time in setting it up.
For some length of B this will win.

S









From:   "P M" <[email protected]>
To:     "International users mailing list for Scilab."
            <[email protected]>
Date:   02/15/2019 03:58 PM
Subject:        [Scilab-users] need a little help
Sent by:        "users" <[email protected]>



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