Skip to content
Permalink
2588d36e56
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
 
 
Cannot retrieve contributors at this time
25 lines (19 sloc) 421 Bytes
function s2 = samplevar(x)
%Input
% x data set (vector)
%
%Output
% s2 variance (scaler)
% s2 = 1/(n-1) * sum((x_i - x_bar)^2)
n = length(x);
% Calculates mean
x_bar = (1/n)*sum(x);
% Creates an empty vector to later be summed together
s2_vec = zeros(1,n);
for i=1:n
%Calculates each component that needs to be summed
s2_vec(i) = (x(i)-x_bar)^2;
end
%Calculates variance
s2 = (1/(n-1))*sum(s2_vec);
end