Basic statistics
Calling the mean
function, we can know the average value of a set of data:
>> a = [11 12 16 23 24 29];
>> mean(a)
ans =
19.1667
We can also pass an array to mean
, which will return the average of each column:
>> A = [1 2 3;4 4 2; 4 2 9];
>> mean(A)
ans =
3.0000 2.6667 4.6667
Since this simple built-in function cannot handle weighted data, we need to process it further. Suppose we have a set of scores and corresponding numbers:
>> x = [55 63 69 70 75 78 82 84 85 88 90 96 100];
>> y = [1 2 1 6 4 7 2 1 3 2 4 1];
According to the knowledge of elementary school, we can get the calculation of the mean:
>> N = sum(y);
>> s = sum(x.*y)
>> aver = s/N
aver =
83.7353
We can also use probability to express:
>> p = y/N;
>> aver = sum(x.*p)
aver =
83.7353