Clustering large data sets might take time,
particularly if you use online updates (set by default). If you have
a Parallel Computing Toolbox™ license and you invoke a pool of workers,
then `kmeans` runs each clustering task (or replicate)
in parallel. Therefore, if `Replicates` > 1, then
the parallel computing decreases time to convergence.

Randomly generate a large data set from a Gaussian mixture
model.

Mu = bsxfun(@times,ones(20,30),(1:20)'); % Gaussian mixture mean
rn30 = randn(30,30);
Sigma = rn30'*rn30; % Symmetric and positive-definite covariance
Mdl = gmdistribution(Mu,Sigma);
rng(1); % For reproducibility
X = random(Mdl,10000);

`Mdl` is a 30-dimensional `gmdistribution` model
with 20 components. `X` is a `10000`-by-`30` matrix
of data generated from `Mdl`.

Invoke a parallel pool of workers. Specify options for
parallel computing.

pool = parpool; % Invokes workers
stream = RandStream('mlfg6331_64'); % Random number stream
options = statset('UseParallel',1,'UseSubstreams',1,...
'Streams',stream);

Starting parallel pool (parpool) using the 'local' profile ... connected to 2 workers.

The input argument `'mlfg6331_64'` of `RandStream` specifies
to use the multiplicative lagged Fibonacci generator algorithm. `options` is
a structure array containing fields that specify options for controlling
estimation.

The Command Window indicates that two workers are available.
The number of workers might vary on your system.

Cluster the data using *k*-means clustering.
Specify that there are *k* = 20 clusters in the data
and increase the number of iterations. Typically, the objective function
contains local minima. Specify 10 replicates to help find a lower,
local minimum.

tic; % Start stopwatch timer
[idx,C,sumd,D] = kmeans(X,20,'Options',options,'MaxIter',10000,...
'Display','final','Replicates',10);
toc % Terminate stopwatch timer

Replicate 4, 121 iterations, total sum of distances = 7.58059e+06.
Replicate 7, 234 iterations, total sum of distances = 7.5904e+06.
Replicate 3, 146 iterations, total sum of distances = 7.59086e+06.
Replicate 2, 179 iterations, total sum of distances = 7.57758e+06.
Replicate 6, 118 iterations, total sum of distances = 7.58614e+06.
Replicate 5, 88 iterations, total sum of distances = 7.59462e+06.
Replicate 1, 99 iterations, total sum of distances = 7.57765e+06.
Replicate 9, 147 iterations, total sum of distances = 7.57639e+06.
Replicate 10, 107 iterations, total sum of distances = 7.60079e+06.
Replicate 8, 144 iterations, total sum of distances = 7.58117e+06.
Best total sum of distances = 7.57639e+06
Elapsed time is 123.857736 seconds.

The Command Window displays the number of iterations and the
terminal objective function value for each replicate. The output arguments
contain the results of replicate `9` because it has
the lowest total sum of distances.