K-d tree: Difference between revisions

m
Line 1,304:
 
=={{header|Julia}}==
<lang julia>usingENV["NN_DEBUG"] = true # or change DEBUG = true in NearestNeighbors.jl file
 
using NearestNeighbors
 
const data = [[2, 3] [5, 4] [9, 6] [4, 7] [8, 1] [7, 2]]
 
NearestNeighbors.reset_stats()
const kdtree = KDTree(Float64.(data))
const indexpoint = [9,2]
idxs, dists = knn(kdtree, indexpoint, 1)
println("Wikipedia example: The nearest neighbor to $indexpoint is ",
"$(data[1:2, idxs[1]]) at distance $(dists).\n")
NearestNeighbors.print_stats()
 
NearestNeighbors.reset_stats()
const cubedis = rand(3, 1000)
const kdcubetree = KDTree(cubedis)
Line 1,319 ⟶ 1,324:
idxs, dists = knn(kdcubetree, rand3point, 1)
 
println("The\n\nThe point $rand3point is closest to the point $(cubedis[1:3, idxs[1]])",
" at distance $(dists[1]).")
NearestNeighbors.print_stats()
</lang>
{{out}}
<pre>
Wikipedia example: The nearest neighbor to [9, 2] is [8, 1] at distance [1.41421].
Nodes visited: 1
Points visited: 6, out of these: 0 unchecked.
The point [0.662566502698; 0.44445872581; 0.0822527927937] is closest to the point [0.621462512343; 0.38924868808; 0.0779214924454] at distance [0.0109268].
Nodes visited: 7
[0.0689667].
</pre>
 
4,105

edits