GRASS Programmer's Manual  6.4.2(2012)
c_median.c
Go to the documentation of this file.
00001 #include <grass/gis.h>
00002 #include <grass/stats.h>
00003 
00004 void c_median(DCELL * result, DCELL * values, int n, const void *closure)
00005 {
00006     n = sort_cell(values, n);
00007 
00008     if (n < 1)
00009         G_set_d_null_value(result, 1);
00010     else
00011         *result = (values[(n - 1) / 2] + values[n / 2]) / 2;
00012 }
00013 
00014 void w_median(DCELL * result, DCELL(*values)[2], int n, const void *closure)
00015 {
00016     DCELL total;
00017     int i;
00018     DCELL k;
00019 
00020     n = sort_cell_w(values, n);
00021 
00022     if (n < 1) {
00023         G_set_d_null_value(result, 1);
00024         return;
00025     }
00026 
00027     total = 0.0;
00028     for (i = 0; i < n; i++)
00029         total += values[i][1];
00030 
00031     k = 0.0;
00032     for (i = 0; i < n; i++) {
00033         k += values[i][1];
00034         if (k >= total / 2)
00035             break;
00036     }
00037 
00038     *result = values[i][0];
00039 }
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines