diff options
author | Samuel Fadel <samuelfadel@gmail.com> | 2015-09-15 13:26:54 -0300 |
---|---|---|
committer | Samuel Fadel <samuelfadel@gmail.com> | 2015-09-15 13:26:54 -0300 |
commit | f0cccff515cd9e94990a3de95a8a2a235bc6d7a8 (patch) | |
tree | d509a1d6db2d25ac172f1df10543a4e7d53e14a5 | |
parent | 2f9d5ebe0af5c39361154270563271c9ce2629f4 (diff) |
Fixed bugs in the kNN algorithm
-rw-r--r-- | measures.cpp | 15 |
1 files changed, 12 insertions, 3 deletions
diff --git a/measures.cpp b/measures.cpp index bee3515..83f4e8a 100644 --- a/measures.cpp +++ b/measures.cpp @@ -10,15 +10,23 @@ void knn(const arma::mat &dmat, arma::uword i, arma::uword k, arma::uvec &nn, ar return; } - double dmax = arma::datum::inf; - nn.fill(i); - dist.fill(dmax); + for (arma::uword j = 0, l = 0; l < k; j++, l++) { + if (j == i) { + j++; + } + + nn[l] = j; + dist[l] = dmat(i, j); + } + + double dmax = *std::max_element(dist.begin(), dist.end()); for (arma::uword j = 0; j < n; j++) { if (j == i) { continue; } if (dmat(i, j) < dmax) { + dmax = dmat(i, j); arma::uword l; for (l = 0; dmat(i, j) > dist[l] && l < k; l++); for (arma::uword m = l + 1; m < k; m++) { @@ -39,6 +47,7 @@ arma::vec mp::neighborhoodPreservation(const arma::mat &distA, arma::uword n = distA.n_rows; arma::vec np(n); + #pragma omp parallel for shared(np, n) for (arma::uword i = 0; i < n; i++) { arma::uvec nnA(k); arma::uvec nnB(k); |