Permalink
Cannot retrieve contributors at this time
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?
MATH-3510/samplevar.m
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
25 lines (19 sloc)
421 Bytes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |