GRASS Programmer's Manual  6.4.2(2012)
c_mode.c
Go to the documentation of this file.
00001 #include <grass/gis.h>
00002 #include <grass/stats.h>
00003 
00004 void c_mode(DCELL * result, DCELL * values, int n, const void *closure)
00005 {
00006     DCELL mode;
00007     int max;
00008     DCELL prev;
00009     int count;
00010     int i;
00011 
00012     n = sort_cell(values, n);
00013 
00014     max = 0;
00015     count = 0;
00016 
00017     for (i = 0; i < n; i++) {
00018         if (max == 0 || values[i] != prev) {
00019             prev = values[i];
00020             count = 0;
00021         }
00022 
00023         count++;
00024 
00025         if (count > max) {
00026             max = count;
00027             mode = prev;
00028         }
00029     }
00030 
00031     if (max == 0)
00032         G_set_d_null_value(result, 1);
00033     else
00034         *result = mode;
00035 }
00036 
00037 void w_mode(DCELL * result, DCELL(*values)[2], int n, const void *closure)
00038 {
00039     DCELL mode;
00040     DCELL max;
00041     DCELL prev;
00042     DCELL count;
00043     int i;
00044 
00045     n = sort_cell_w(values, n);
00046 
00047     max = 0.0;
00048     count = 0.0;
00049 
00050     for (i = 0; i < n; i++) {
00051         if (max == 0.0 || values[i][0] != prev) {
00052             prev = values[i][0];
00053             count = 0.0;
00054         }
00055 
00056         count += values[i][1];
00057 
00058         if (count > max) {
00059             max = count;
00060             mode = prev;
00061         }
00062     }
00063 
00064     if (max == 0.0)
00065         G_set_d_null_value(result, 1);
00066     else
00067         *result = mode;
00068 }
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines