Open In App

Count Occurrences of Categorical Array Elements By Category

Last Updated : 01 May, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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. 

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.

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.

Matlab




%categorical array
arr = categorical({'cat1', 'cat2', 'cat1', 'cat4'});
%getting category count
count = countcats(arr);
%displaying category count with categories
categories(arr)
fprintf('Count of categories respectively:')
disp(count)


In this code, we created a categorical array with 3 categories. Then we counted the categories with the countcats function. The countcats function only returns the count of categories but, in the same sequences as the categories will be shown by the categories function (which returns the unique categories in a categorical array). Then, we displayed the categories and their count.

The output of the same is:

 

Example 2

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:

Matlab




%categorical array
arr = categorical({'cat1', 'cat2'; 'cat1', 'cat4'});
%getting category count
count = countcats(arr);
%displaying category count with categories
categories(arr)
disp(count)


Here we have created a 2×2 matrix with three categories. Now, when we call the countcats function on this matrix, we will get the number of times every category is repeated in each column of the matrix. 

 

As we can see here, the categories are displayed in a sequence. Then, in the same sequence, countcats returns the categorical count. We get a 3×2 matrix for counts as there are 3 categories(each one representing a row) and 2 columns representing the respective columns in the matrix.
Now, the first category occurs 2 times in first column and 0 times in second column, the same is shown in the output. Similarly, we can check the counts of other two categories as well.

Example 3

In this last example, we shall see how to get the categorical counts in a particular dimension. We have done the same thing in previous example, where we got the count columnwise, which is the default dimension. We will be using the same matrix for demonstration and then, pass the dimension as the input to the countcats function.

Matlab




%categorical array
arr = categorical({'cat1', 'cat2'; 'cat1', 'cat4'});
%getting category count
count = countcats(arr,2);
%displaying category count with categories
categories(arr)
disp(count)


Here we use the same matrix as previous example but, instead of getting the columnwise count of categories, we are finding the rowwise count, or along the second dimension. To specify the second dimension, we have added the optional parameter for dimension in the countcats. In the case the dimension is 2. 

 

Here, we get a 2×3 matrix in return as there are only two rows, representing each row of original matirx and 3 columns, each representing a category in same order as they are displayed by the categories functions. 

We can verify that the first category ‘cat1’, comes in each row exactly one time. The ‘cat2’ category only occurs in first row and the ‘cat4’ category occurs only in the second row.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads