GRASS Programmer's Manual  6.5.svn(2014)-r66266
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
c_mode.c
Go to the documentation of this file.
1 #include <grass/gis.h>
2 #include <grass/stats.h>
3 
4 void c_mode(DCELL * result, DCELL * values, int n, const void *closure)
5 {
6  DCELL mode;
7  int max;
8  DCELL prev;
9  int count;
10  int i;
11 
12  n = sort_cell(values, n);
13 
14  max = 0;
15  count = 0;
16 
17  for (i = 0; i < n; i++) {
18  if (max == 0 || values[i] != prev) {
19  prev = values[i];
20  count = 0;
21  }
22 
23  count++;
24 
25  if (count > max) {
26  max = count;
27  mode = prev;
28  }
29  }
30 
31  if (max == 0)
32  G_set_d_null_value(result, 1);
33  else
34  *result = mode;
35 }
36 
37 void w_mode(DCELL * result, DCELL(*values)[2], int n, const void *closure)
38 {
39  DCELL mode;
40  DCELL max;
41  DCELL prev;
42  DCELL count;
43  int i;
44 
45  n = sort_cell_w(values, n);
46 
47  max = 0.0;
48  count = 0.0;
49 
50  for (i = 0; i < n; i++) {
51  if (max == 0.0 || values[i][0] != prev) {
52  prev = values[i][0];
53  count = 0.0;
54  }
55 
56  count += values[i][1];
57 
58  if (count > max) {
59  max = count;
60  mode = prev;
61  }
62  }
63 
64  if (max == 0.0)
65  G_set_d_null_value(result, 1);
66  else
67  *result = mode;
68 }
int sort_cell_w(DCELL(*array)[2], int n)
Definition: sort_cell.c:34
void w_mode(DCELL *result, DCELL(*values)[2], int n, const void *closure)
Definition: c_mode.c:37
void G_set_d_null_value(DCELL *dcellVals, int numVals)
Definition: null_val.c:176
int count
#define max(x, y)
Definition: draw2.c:69
int sort_cell(DCELL *array, int n)
Definition: sort_cell.c:21
void c_mode(DCELL *result, DCELL *values, int n, const void *closure)
Definition: c_mode.c:4
int n
Definition: dataquad.c:291
tuple mode
Definition: tools.py:1481