Skip to content

Instantly share code, notes, and snippets.

@ninjakx
Created July 26, 2019 05:22
Show Gist options
  • Save ninjakx/556f9151a99f7c311809f2cd79328b5c to your computer and use it in GitHub Desktop.
Save ninjakx/556f9151a99f7c311809f2cd79328b5c to your computer and use it in GitHub Desktop.
#include <cstdlib>
#include <algorithm>
#include <numeric>
#include <vector>
#include <stdio.h>
#include <iostream>
#include <map>
#include <cmath>
#include <math.h>
std::vector<int> argsort(double* input_list, int length) {
std::vector<int> out(length);
iota(out.begin(), out.end(), 0);
sort(out.begin(), out.end(),
[&input_list](int i1, int i2) {return input_list[i1] < input_list[i2];});
std::vector <int> v2(out.begin()+1 , out.end());
return v2;
}
double edist(double* arr1, double* arr2, int n) {
double sum = 0.0;
for (int i=0; i<n; i++) {
sum += pow(arr1[i] - arr2[i], 2);
}
return sqrt(sum);
}
std::map<int, std::vector<int> > distance_knn(const double *array,int N, int M) {
std::map<int, std::vector<int> > dist;
double **arr = new double*[N];
for (int i = 0; i < N; i++) {
arr[i] = new double[M];
for(int j=0; j < M; j++) {
arr[i][j] = array[i*M+j];
}
}
for (int i=0; i<N; i++) {
double distances[N];
for(int j=0; j<N; j++) {
// distances.push_back();
distances[j] = edist(arr[i], arr[j], M);
}
dist[i] = argsort(distances, N);
}
return dist;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment