/* nag_mv_kmeans_cluster_analysis (g03efc) Example Program. * * Copyright 1998 Numerical Algorithms Group. * * Mark 5, 1998. * Mark 7, revised, 2001. * */ #include #include #include #include #define NMAX 20 #define MMAX 5 #define KMAX 3 int main(void) { double cmeans[KMAX][MMAX], css[KMAX], csw[KMAX], wt[NMAX], x[NMAX][MMAX]; double *wtptr; Integer nvar, i, j, k; Integer m, n; Integer inc[NMAX], isx[MMAX], nic[KMAX]; Integer maxit; Integer tdc=MMAX, tdx=MMAX; char weight[2]; Vprintf("g03efc Example Program Results\n\n"); /* Skip heading in the data file */ Vscanf("%*[^\n]"); Vscanf("%s",weight); Vscanf("%ld",&n); Vscanf("%ld",&m); Vscanf("%ld",&nvar); Vscanf("%ld",&k); Vscanf("%ld",&maxit); if (n <= NMAX && m <= MMAX) { if (*weight == 'W') { for (i = 0; i < n; ++i) { for (j = 0; j < m; ++j) Vscanf("%lf",&x[i][j]); Vscanf("%lf",&wt[i]); } wtptr = wt; } else { for (i = 0; i < n; ++i) { for (j = 0; j < m; ++j) Vscanf("%lf",&x[i][j]); } wtptr = 0; } for (i = 0; i < k; ++i) { for (j = 0; j < nvar; ++j) Vscanf("%lf",&cmeans[i][j]); } for (j = 0; j < m; ++j) Vscanf ("%ld",&isx[j]); g03efc(n, m, &x[0][0], tdx, isx, nvar, k, &cmeans[0][0], tdc, wtptr, inc, nic, css, csw, maxit, NAGERR_DEFAULT); Vprintf("\nThe cluster each point belongs to\n"); for (i = 0; i < n; ++i) Vprintf(" %6ld%s",inc[i], (i+1)%10 ? "" : "\n"); Vprintf("\n\nThe number of points in each cluster\n"); for (i = 0; i < k; ++i) Vprintf(" %6ld",nic[i]); Vprintf("\n\nThe within-cluster sum of weights of each cluster\n"); for (i = 0; i < k; ++i) Vprintf(" %9.2f",csw[i]); Vprintf("\n\nThe within-cluster sum of squares of each cluster\n\n"); for (i = 0; i < k; ++i) Vprintf(" %13.4f",css[i]); Vprintf("\n\nThe final cluster centres\n"); Vprintf(" 1 2 3 4 5\n"); for (i = 0; i < k; ++i) { Vprintf(" %5ld ",i+1); for (j = 0; j < nvar; ++j) Vprintf("%8.4f",cmeans[i][j]); printf("\n"); } return EXIT_SUCCESS; } else { Vprintf("Incorrect input value of n or m.\n"); return EXIT_FAILURE; } }