ndim = barttest(x,alpha) returns
the number of dimensions necessary to explain the nonrandom variation
in the data matrix x at the alpha significance
level.

[ndim,prob,chisquare]
= barttest(x,alpha) also
returns the significance values for the hypothesis tests prob,
and the χ^{2} values
associated with the tests chisquare.

Generate a 20-by-6 matrix of random numbers from a multivariate normal distribution with mean mu = [0 0] and covariance sigma = [1 0.99; 0.99 1].

rng default% for reproducibility
mu = [0 0];
sigma = [1 0.99; 0.99 1];
X = mvnrnd(mu,sigma,20); % columns 1 and 2
X(:,3:4) = mvnrnd(mu,sigma,20); % columns 3 and 4
X(:,5:6) = mvnrnd(mu,sigma,20); % columns 5 and 6

Determine the number of dimensions necessary to explain the nonrandom variation in data matrix X. Report the significance values for the hypothesis tests.

Number of dimensions, returned as a positive integer value.
The dimension is determined by a series of hypothesis tests. The test
for ndim = 1 tests the hypothesis that the variances
of the data values along each principal component are equal, the test
for ndim = 2 tests the hypothesis that the variances
along the second through last components are equal, and so on. The
null hypothesis is that the number of dimensions is equal to the number
of the largest unequal eigenvalues of the covariance matrix of x.

Significance value for the hypothesis tests, returned as a vector
of scalar values in the range (0,1). Each element
in prob corresponds to an element of chisquare.