c_mode.c 991 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. #include <grass/gis.h>
  2. #include <grass/stats.h>
  3. void c_mode(DCELL * result, DCELL * values, int n)
  4. {
  5. DCELL mode;
  6. int max;
  7. DCELL prev;
  8. int count;
  9. int i;
  10. n = sort_cell(values, n);
  11. max = 0;
  12. count = 0;
  13. for (i = 0; i < n; i++) {
  14. if (max == 0 || values[i] != prev) {
  15. prev = values[i];
  16. count = 0;
  17. }
  18. count++;
  19. if (count > max) {
  20. max = count;
  21. mode = prev;
  22. }
  23. }
  24. if (max == 0)
  25. G_set_d_null_value(result, 1);
  26. else
  27. *result = mode;
  28. }
  29. void w_mode(DCELL * result, DCELL(*values)[2], int n)
  30. {
  31. DCELL mode;
  32. DCELL max;
  33. DCELL prev;
  34. DCELL count;
  35. int i;
  36. n = sort_cell_w(values, n);
  37. max = 0.0;
  38. count = 0.0;
  39. for (i = 0; i < n; i++) {
  40. if (max == 0.0 || values[i][0] != prev) {
  41. prev = values[i][0];
  42. count = 0.0;
  43. }
  44. count += values[i][1];
  45. if (count > max) {
  46. max = count;
  47. mode = prev;
  48. }
  49. }
  50. if (max == 0.0)
  51. G_set_d_null_value(result, 1);
  52. else
  53. *result = mode;
  54. }