If your task is actually 2D, consider complex numbers. This version
performs better on the sample data P=:p
nearest_neighbors =: {~ convert_to_index@:identify_minimum@:separation
separation =: [: }. <@:|@:({: - }:)\&:(j./"1)
identify_minimum =: [: ; (i.L:0 <./@:;)
convert_to_index=:(0 1 + ] , i.) (#~ (<: i.@:#))
NB. examples by part
boxdraw_j_ 1
separation 4{.P
+-------+---------------+-----------------------+
|3.71611|6.01287 6.83522|1.30641 4.14391 7.31405|
+-------+---------------+-----------------------+
identify_minimum separation 4{.P
1 2 0
convert_to_index identify_minimum separation 4{.P
0 3
nearest_neighbors 4{.P
6.42201 5.83321
6.00479 7.07121
nearest_neighbors P
6.42201 5.83321
6.62593 6.08499
NB. time & space
close=: 3 :'y #~ +/@(= [: <./ 0-.~,)+/"1 *: -"1/~y' NB. RDM
flose=:nearest_neighbors f.
999 timespacex"0 1 'close P',:'flose P'
9.15946e_5 39680
5.26036e_5 10240
Date: Wed, 16 Sep 2015 19:49:56 +0000 (UTC)
From: "'Pascal Jasmin' via Programming"<[email protected]>
To: Programming Forum<[email protected]>
Subject: [Jprogramming] An adverb for a loopy algorithm
Message-ID:
<[email protected]>
Content-Type: text/plain; charset=UTF-8
finding the 2 closest points in a set, has a loopy algorithm of "for each point,
compare it with all of the points to its right"
p =: 0 ". every cutLF 0 : 0
6.42201 5.83321
3.15448 4.06327
8.89456 0.352235
6.00479 7.07121
8.10462 9.19487
9.63448 4.00534
6.74378 0.791349
5.56034 9.27039
4.67282 8.45993
0.301042 9.4069
6.62593 6.08499
9.0307 2.37372
9.36324 1.80147
2.67396 1.62207
4.76667 1.94554
7.43839 6.05369
)
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm