A = categorical({'plane''car''train''car''plane'})

A =
plane car train car plane

A has three categories, car, plane,
and train.

Find the number of elements in each category of A.

B = countcats(A)

B =
2 2 1

The first element in B corresponds to the
first category of A, which is car.
The second element in B corresponds to the second
category of A, which is plane.
The third element of B corresponds to the third
category of A, which is train.

Since A is a row vector, countcats returns
a row vector.

The first row of B corresponds to the first
category of A. The value, red,
occurs once in the first column of A and twice
in the second column.

The second row of B corresponds to the second
category of A. The value, green,
occurs once in the first column of A, and it does
not occur in the second column.

The third row of B corresponds to the third
category of A. The value, blue,
occurs once in the first column of A and once in
the second column.

Find the category counts of A along
the second dimension.

B = countcats(A,2)

B =
1 0 1
1 1 0
1 0 1

The first column of B corresponds to the
first category of A. The value, red,
occurs once in the first row of A, once in the
second row, and once in the third row.

The second column of B corresponds to the
second category of A. The value, green,
occurs in only one element. It occurs in the second row of A.

The third column of B corresponds to the
third category of A. The value, blue,
occurs once in the first row of A and once in the
third row.

Dimension to operate along, specified as a positive integer
scalar. If no value is specified, the default is the first array dimension
whose size does not equal 1.

Consider a two-dimensional categorical array, A.

If dim = 1, then countcats(A,1) returns
the category counts for each column of A.

If dim = 2, then countcats(A,2) returns
the category counts of each row of A.

If dim is greater than ndims(A),
then countcats(A) returns an array the same size
as A for each category. countcats returns 1 for
elements in the corresponding category and 0 otherwise.