GRASS Programmer's Manual  6.4.2(2012)
c_assign.c
Go to the documentation of this file.
00001 #include <math.h>
00002 #include <grass/cluster.h>
00003 
00004 int I_cluster_assign(struct Cluster *C, int *interrupted)
00005 {
00006     int p, c;
00007     int class, band;
00008     double d, q;
00009     double dmin;
00010 
00011     /*
00012        fprintf (stderr,"I_cluster_assign(npoints=%d,nclasses=%d,nbands=%d)\n",
00013        C->npoints, C->nclasses, C->nbands);
00014      */
00015 
00016     for (p = 0; p < C->npoints; p++) {
00017         if (*interrupted)
00018             return -1;
00019 
00020         dmin = HUGE_VAL;
00021         class = 0;
00022         for (c = 0; c < C->nclasses; c++) {
00023             d = 0.0;
00024             for (band = 0; band < C->nbands; band++) {
00025                 q = C->points[band][p];
00026                 q -= C->mean[band][c];
00027                 d += q * q;
00028             }
00029             if (c == 0 || d < dmin) {
00030                 class = c;
00031                 dmin = d;
00032             }
00033         }
00034         C->class[p] = class;
00035         C->count[class]++;
00036         for (band = 0; band < C->nbands; band++)
00037             C->sum[band][class] += C->points[band][p];
00038     }
00039 
00040     return 0;
00041 }
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines