| Products & Services | Solutions | Academia | Support | User Community | Company |
| Download Product Updates | | | Get Pricing | | | Trial Software |
| Documentation → Statistics Toolbox |
| Contents | Index |
| Learn more about Statistics Toolbox |
h = ttest(x)
h = ttest(x,m)
h = ttest(x,y)
h = ttest(...,alpha)
h = ttest(...,alpha,tail)
h = ttest(...,alpha,tail,dim)
[h,p] = ttest(...)
[h,p,ci] = ttest(...)
[h,p,ci,stats] = ttest(...)
h = ttest(x) performs a t-test of the null hypothesis that data in the vector x are a random sample from a normal distribution with mean 0 and unknown variance, against the alternative that the mean is not 0. The result of the test is returned in h. h = 1 indicates a rejection of the null hypothesis at the 5% significance level. h = 0 indicates a failure to reject the null hypothesis at the 5% significance level.
x can also be a matrix or an N-dimensional array. For matrices, ttest performs separate t-tests along each column of x and returns a vector of results. For N-dimensional arrays, ttest works along the first non-singleton dimension of x.
The test treats NaN values as missing data, and ignores them.
h = ttest(x,m) performs a t-test of the null hypothesis that data in the vector x are a random sample from a normal distribution with mean m and unknown variance, against the alternative that the mean is not m.
h = ttest(x,y) performs a paired t-test of the null hypothesis that data in the difference x-y are a random sample from a normal distribution with mean 0 and unknown variance, against the alternative that the mean is not 0. x and y must be vectors of the same length, or arrays of the same size.
h = ttest(...,alpha) performs the test at the (100*alpha)% significance level. The default, when unspecified, is alpha = 0.05.
h = ttest(...,alpha,tail) performs the test against the alternative specified by the string tail. There are three options for tail:
'both' — Mean is not 0 (or m) (two-tailed test). This is the default, when tail is unspecified.
'right' — Mean is greater than 0 (or m) (right-tail test)
'left' — Mean is less than 0 (or m) (left-tail test)
tail must be a single string, even when x is a matrix or an N-dimensional array.
h = ttest(...,alpha,tail,dim) works along dimension dim of x, or of x-y for a paired test. Use [] to pass in default values for m, alpha, or tail.
[h,p] = ttest(...) returns the p-value of the test. The p-value is the probability, under the null hypothesis, of observing a value as extreme or more extreme of the test statistic
![]()
where
is the sample mean, μ =
0 (or m) is the hypothesized population mean, s is
the sample standard deviation, and n is the sample
size. Under the null hypothesis, the test statistic will have Student's t distribution
with n – 1 degrees of freedom.
[h,p,ci] = ttest(...) returns a 100*(1 – alpha)% confidence interval on the population mean, or on the difference of population means for a paired test.
[h,p,ci,stats] = ttest(...) returns the structure stats with the following fields:
tstat — Value of the test statistic
df — Degrees of freedom of the test
sd — Sample standard deviation
Simulate a random sample of size 100 from a normal distribution with mean 0.1:
x = normrnd(0.1,1,1,100);
Test the null hypothesis that the sample comes from a normal distribution with mean 0:
[h,p,ci] = ttest(x,0)
h =
0
p =
0.8323
ci =
-0.1650 0.2045The test fails to reject the null hypothesis at the default α = 0.05 significance level. Under the null hypothesis, the probability of observing a value as extreme or more extreme of the test statistic, as indicated by the p-value, is much greater than α. The 95% confidence interval on the mean contains 0.
Simulate a larger random sample of size 1000 from the same distribution:
y = normrnd(0.1,1,1,1000);
Test again if the sample comes from a normal distribution with mean 0:
[h,p,ci] = ttest(y,0)
h =
1
p =
0.0160
ci =
0.0142 0.1379This time the test rejects the null hypothesis at the default α = 0.05 significance level. The p-value has fallen below α = 0.05 and the 95% confidence interval on the mean does not contain 0.
Because the p-value of the sample y is greater than 0.01, the test will fail to reject the null hypothesis when the significance level is lowered to α = 0.01:
[h,p,ci] = ttest(y,0,0.01)
h =
0
p =
0.0160
ci =
-0.0053 0.1574Notice that at the lowered significance level the 99% confidence interval on the mean widens to contain 0.
This example will produce slightly different results each time it is run, because of the random sampling.
![]() | tstat | ttest2 | ![]() |

Includes the most popular MATLAB recorded presentations with Q&A sessions led by MATLAB experts.
| © 1984-2009- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |