MATLAB: How to calculate the confidence interval

confidence intervalconfidence intervallmeant-distribution

Hi
I have a vector x with e.g. 100 data point. I can easy calculate the mean but now I want the 95% confidence interval. I can calculate the 95% confidence interval as follows:
CI = mean(x)+- t * (s / square(n))
where s is the standard deviation and n the sample size (= 100).
Is there a method in matlab where I just can feed in the vector and then I get the confidence interval?
Or I can write my own method but I need at least the value of t (critical value of the t distribution) because it depends on the number of samples and I don't want to lookup it in a table everytime. Is this possible?
Would be very nice if somebody could give an example.
Last but not least, I want 95% confidence in a 5% interval around the mean. For checking that I just have to calculate the 95% confidence interval and then check if the retrieved value is less than 5% of my mean, right?

Best Answer

This works:
x = randi(50, 1, 100); % Create Data
SEM = std(x)/sqrt(length(x)); % Standard Error
ts = tinv([0.025 0.975],length(x)-1); % T-Score
CI = mean(x) + ts*SEM; % Confidence Intervals
You have to have the Statistics Toolbox to use the tinv function. If you do not have it, I can provide you with a few lines of my code that will calculate the t-probability and its inverse.