6dc150e3f0556deec5cdd82c61e6b120bd905da3
3 #include "sources/kmeansClustering.h"
4 #include <cgds/Vector.h>
6 // k-means based on a distance matrix (nstart=10, maxiter=100)
7 SEXP
kmeansWithDistances(
13 // get scalar arguments
14 int K
= INTEGER_VALUE(K_
);
15 int nstart
= NUMERIC_VALUE(nstart_
);
16 int maxiter
= INTEGER_VALUE(maxiter_
);
18 // extract infos from M and get associate pointer
19 SEXP dim
= getAttrib(distances_
, R_DimSymbol
);
20 int n
= INTEGER(dim
)[0];
21 double* pDistances
= REAL(distances_
);
23 // Main call to core algorithm
24 int* clusters
= kmeansWithDistances_core(pDistances
, n
, K
, nstart
, maxiter
);
26 // allocations and recopies to R vector object
28 PROTECT(bestClusts
= allocVector(INTSXP
, n
));
29 int* pBestClusts
= INTEGER(bestClusts
);
30 for (int i
=0; i
<n
; i
++)
31 pBestClusts
[i
] = clusters
[i
] + 1; // add 1 to start labels at 1
34 // and return clusters