how to calculate t-score in matlab

main.m
% suppose we have two arrays of data: data1 and data2
% n1 and n2 are sample sizes
% alpha is the level of significance, typically 0.05

% calculate means and standard deviations
mean1 = mean(data1);
mean2 = mean(data2);
std1 = std(data1);
std2 = std(data2);

% calculate the standard error
SE = sqrt((std1^2)/n1 + (std2^2)/n2);

% calculate the t-value
t = (mean1 - mean2) / SE;

% calculate the degrees of freedom
df = n1 + n2 - 2;

% calculate the critical t-value
t_critical = tinv(1-alpha/2, df);

% compare t with t_critical and reject or fail to reject null hypothesis
if (abs(t) > t_critical)
    disp('Reject null hypothesis');
else
    disp('Fail to reject null hypothesis');
end
690 chars
29 lines

gistlibby LogSnag