Friday, June 30, 2017

Median vs. Average, Arduino Calculations

If we want to average a set of numbers, we add them together, then divide by the number in the set.

150 + 200 + 0 = 350 / 3 = 116 Average

I'm using int and dropping the fractional parts. Use float if you need them.

But if we want the Median, which is the center, or middle number, we have to sort and compare each number to the others to get a list from high to low, and grab the center number. In this case, the Median is 150.

I've written a sketch that calculates both so you can compare your own numbers, and make it a much larger list:

int a[] = {150,200,0};
int n = 3;

a[] is your set of values

n is the number of values in the set

https://pastebin.com/Ge0pZGS2

More info on Mean, Median & Average - https://www.vocabulary.com/articles/chooseyourwords/mean-median-average/





No comments:

Post a Comment