The countcats function

MATLAB provides the countcats function to count the categories in a categorical array. This function only works on categorical arrays and will throw an error on ordinary arrays or vectors. The syntax of the same is:

cnt = countcats(<categorical-array-name>)

The syntax is simple as the function takes the required parameter as the name of the categorical array whose categories need to be counted. In case of multidimensional categorical arrays, we can also pass the dimension along which we need the category count. We shall now see the same in example.

Count Occurrences of Categorical Array Elements By Category

Categorical arrays are arrays that stores data of categorical type in MATLAB. The categorical data is pre defined by a finite set of discrete values/categories from which any category can be selected multiple times. 

In this article, we shall learn how to count the occurrences of discrete categories in a categorical array in MATLAB. This can come handy when we need to count the number of times a category repeats in our data or we can also use this method to find whether or not we have unique entries in some data. 

Similar Reads

The countcats function

MATLAB provides the countcats function to count the categories in a categorical array. This function only works on categorical arrays and will throw an error on ordinary arrays or vectors. The syntax of the same is:...

Example 1

In this example, we shall how the countcats function works on a categorical vector. First we shall create a simple categorical array and then perform the category count on it....

Example 2

...

Example 3

Now we shall find the categories for a categorical matrix. We will be using the same three categories and then create a categorical matrix with them. The countcats function work differently on a matrix as it returns the counts of categories in each column. See the code below:...