forked from parallella/pal
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathp_mode.c
More file actions
44 lines (39 loc) · 1 KB
/
p_mode.c
File metadata and controls
44 lines (39 loc) · 1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <pal.h>
/**
*
* Calculates the mode value of input vector 'a'.
*
* @param a Pointer to input vector
*
* @param c Pointer to output scalar
*
* @param n Size of 'a' vector.
*
* @return None
*
*/
void p_mode_f32(const float *a, float *c, int n)
{
unsigned int occurrence_count = 0;
unsigned int max_occurrence_count = 0;
unsigned int i = 1;
float mode_value = 0.0f;
float *sorted_a = (float*) malloc(sizeof(float) * n);
p_sort_f32(a, sorted_a, n);
for (; i < n; ++i) {
++occurrence_count;
if (sorted_a[i] != sorted_a[i - 1]) {
if (occurrence_count > max_occurrence_count) {
max_occurrence_count = occurrence_count;
mode_value = sorted_a[i - 1];
}
occurrence_count = 0;
}
}
if (occurrence_count > max_occurrence_count) {
max_occurrence_count = occurrence_count;
mode_value = sorted_a[n - 1];
}
*c = mode_value;
p_free(sorted_a);
}