aboutsummaryrefslogtreecommitdiff
path: root/knn.cpp
diff options
context:
space:
mode:
authorSamuel Fadel <samuelfadel@gmail.com>2015-10-22 16:39:15 -0200
committerSamuel Fadel <samuelfadel@gmail.com>2015-10-22 16:39:15 -0200
commitad4fbabeca2cbdf4cb47f1a923183027494ab0a8 (patch)
tree4a9c16454ff4d802b23a8271f4005ee1846f7b1b /knn.cpp
parent99ac0af03e1695ba4de2c42e949fce61b84850e5 (diff)
Added PLMP as an alternative technique to use; knn() is now exposed in the mp namespace.
Diffstat (limited to 'knn.cpp')
-rw-r--r--knn.cpp30
1 files changed, 30 insertions, 0 deletions
diff --git a/knn.cpp b/knn.cpp
new file mode 100644
index 0000000..579a76f
--- /dev/null
+++ b/knn.cpp
@@ -0,0 +1,30 @@
+#include "mp.h"
+
+void mp::knn(const arma::mat &dmat, arma::uword i, arma::uword k, arma::uvec &nn, arma::vec &dist)
+{
+ arma::uword n = dist.n_rows;
+ double dmax = arma::datum::inf;
+ nn.fill(i);
+ dist.fill(dmax);
+ if (k > n) {
+ return;
+ }
+
+ const arma::vec &dvec = dmat.col(i);
+ for (arma::uword j = 0; j < n; j++) {
+ if (j == i || dvec[j] > dmax) {
+ continue;
+ }
+
+ arma::uword l;
+ for (l = 0; dist[l] < dvec[j] && l < k; l++);
+ for (arma::uword m = k - 1; m > l; m--) {
+ nn[m] = nn[m - 1];
+ dist[m] = dist[m - 1];
+ }
+
+ nn[l] = j;
+ dist[l] = dvec[j];
+ dmax = dist[k - 1];
+ }
+}