Create a confusion matrix chart by using the `confusionchart`

function, and sort the classes to cluster similar classes by using the `'cluster'`

option of the `sortClasses`

function. This example also shows how to cluster by using the `pdist`

, `linkage`

, and `optimalleaforder`

functions.

Generate a sample data set that contains eight distinct classes.

Insert confusion among classes {1,4,7}, {2,8}, and {5,6} for the first 200 samples.

Create a confusion matrix chart from the true labels `trueLabels`

and the predicted labels `predictedLabels`

.

**Cluster Using **`'cluster'`

Sort the classes to cluster similar classes by using the `'cluster'`

option.

**Cluster Using **`pdist`

, `linkage`

, and `optimalleaforder`

Instead of using the `'cluster'`

option, you can use the `pdist`

, `linkage`

, and `optimalleaforder`

functions to cluster confusion matrix values. You can customize clustering by using the options of these functions. For details, see the corresponding function reference pages.

Suppose you have a confusion matrix and class labels.

Compute the clustered matrix and find the corresponding class labels by using `pdist`

, `linkage`

, and `optimalleaforder`

. The `pdist`

function computes the Euclidean distance `D`

between pairs of the confusion matrix values. The `optimalleaforder`

function returns an optimal leaf ordering for the hierarchical binary cluster tree `linkage(D)`

using the distance `D`

.

Create a confusion matrix chart using the clustered matrix and the corresponding class labels. Then, sort the classes using the class labels.

The sorted confusion matrix chart `cm2`

, which you created by using `pdist`

, `linkage`

, and `optimalleaforder`

, is identical to the sorted confusion matrix chart `cm1`

, which you created by using the `'cluster'`

option.