user1759208
user1759208

Reputation: 41

Calculating standard deviation using data points and count vector in Matlab

I want to use Matlab to calculate the standard deviation of a population I have compiled.

The matlab function takes as input one large population vector and outputs the standard dev.

However, for optimisation purposes, instead of one large vector, I have a set of individual data points, and the number of times each point is used.

I could use a loop and create a huge population vector, but it's not ideal.

How could I go about this?

Upvotes: 2

Views: 1026

Answers (1)

Luis Mendo
Luis Mendo

Reputation: 112699

Very easy from the definition of standard deviation: just introduce weights to account for the number of repetitions of each data point:

data  = [1 3 4 2 4]; %// data values
count = [4 5 4 5 8]; %// number of times for each value

mu = sum(data.*count)./(sum(count));
dev = sqrt(sum((data-mu).^2.*count)./(sum(count)-1)); %// or ./sum(count)

Upvotes: 3

Related Questions