1D Categorical Arrays
When we call the summary function on a categorical array, it returns the category count of that array. See the following example.
Matlab
% creating a categorical array cat = categorical({ 'red' 'bus' 'ship' 'bus' 'bike' 'car' 'red' }); % calling the summary function summary(cat) |
Output:
>> summary(cat)
bike bus car red ship
1 2 1 2 1
Firstly, we have created a categorical array and then, we called the summary function on it. The output will give us the category count along with the names of the categories.
2D Categorical Arrays
When we call the summary function on a 2D categorical array, it returns the category count along a particular dimension. The default dimension value is 1 i.e., column-wise. Let’s see how this work.
Matlab
% 2D categorical array cat = categorical({ 'red' 'bus' ; 'ship' 'bus' ; 'bike' 'car' ; 'red' 'ship' }); % calling the summary function summary(cat) |
Output:
>> summary(cat)
bike 1 0
bus 0 2
car 0 1
red 2 0
ship 1 1
Here, we have created a 2D categorical array with 5 categories. When we call the summary function on this array, it will display the column-wise category count, which is the default option.
Now, we can also retrieve the summary row-wise or along dimension 2. For doing so, we only need to pass the dimension value as 2. See the following code:
Matlab
% 2D categorical array cat = categorical({ 'red' 'bus' ; ... 'ship' 'bus' ; ... 'bike' 'car' ; ... 'red' 'ship' }); % rowwise summary summary(cat,2) |
Output:
>> summary(cat,2)
bike bus car red ship
0 1 0 1 0
0 1 0 0 1
1 0 1 0 0
0 0 0 1 1
This code will print the summary (in this case the category count) row-wise. We can clearly verify that all the category counts are given row-wise.
Print summary of table, timetable, or categorical array in MATLAB
MATLAB provides various ways to store and organize data such as tables, categorical arrays, arrays, timetables, vectors, etc. MATLAB deals with big data used for machine learning and other mathematical functions which require different organization of data. For small data it is efficient to take a look at the entire data and summarize it but, the same cannot be done when you are working with big data or binary data(.mat files).
For such situations, MATLAB provides the summary function which returns a brief summary of the data structure passed to it. It must be noted here that the summary function works only on three type of MATLAB objects:
- Table
- Timetable
- Categorical Array/Vector
In this article, we shall see how to use the summary function on the above-mentioned MATLAB objects.