GRASS GIS 8 Programmer's Manual  8.5.0dev(2024)-76958057df
c_means.c
Go to the documentation of this file.
1 /*!
2  \file cluster/c_means.c
3
4  \brief Cluster library - Means value
5
6  (C) 2001-2009 by the GRASS Development Team
7
8  This program is free software under the GNU General Public License
9  (>=v2). Read the file COPYING that comes with GRASS for details.
10
11  \author Original author CERL
12  */
13
14 #include <math.h>
15 #include <grass/cluster.h>
16
17 /*!
18  \brief Calculate means value
19
20  \param C pointer to Cluster structure
21
22  \return 0
23  */
24 int I_cluster_means(struct Cluster *C)
25 {
26  int band;
27  int class;
28  double m, v; /* m=mean, v=variance then std dev */
29  double s;
30
31  G_debug(3, "I_cluster_means(nbands=%d,nclasses=%d)", C->nbands,
32  C->nclasses);
33
34  for (band = 0; band < C->nbands; band++) {
35  s = C->band_sum[band];
36  m = s / C->npoints;
37  v = C->band_sum2[band] - s * m;
38  v = sqrt(v / (C->npoints - 1));
39  for (class = 0; class < C->nclasses; class ++)
40  C->mean[band][class] = m;
41  if (C->nclasses > 1)
42  for (class = 0; class < C->nclasses; class ++)
43  C->mean[band][class] +=
44  ((2.0 * class) / (C->nclasses - 1) - 1.0) * v;
45  }
46
47  return 0;
48 }
int I_cluster_means(struct Cluster *C)
Calculate means value.
Definition: c_means.c:24
int G_debug(int, const char *,...) __attribute__((format(printf
float mean(IClass_statistics *statistics, int band)
Helper function for computing mean.
Definition: cluster.h:7
int npoints
Definition: cluster.h:9
double * band_sum2
Definition: cluster.h:14
int nclasses
Definition: cluster.h:26
double * band_sum
Definition: cluster.h:13
int nbands
Definition: cluster.h:8