Categorical arrays are arrays of qualitative data whose values are taken from a finite set consisting of discrete data and non-numeric data.
Creating Categorical Arrays
[Example 2-60] Use the categorical function to create a categorical array.
Enter in the Command Window:
S=categorical({'大';'小';'中';'小';'大'})
S =
大
小
中
小
大
[Example 2-61] Use the categories function to display categories. Categories are distinguished by unique element values in the categorical array.
Enter in the Command Window:
categories(S)
ans =
'中'
'大'
'小'
[Example 2-62] Use the categorical function to create an ordinal categorical array.
First create a numeric array below:
A=[1 3;2 1;2 3]
A =
1 3
2 1
2 3
Then use the categorical function to convert A to an ordinal categorical array, where 1, 2, and 3 represent the categories '少年' (Youth), '青年' (Young Adult), and '中年' (Middle-aged), respectively.
valueset=[1:3];
catnames={'少年','青年','中年'};
B=categorical(A,valueset,catnames,'Ordinal',true)
B =
少年 中年
青年 少年
青年 中年
Use the iscategorical function to determine if the input is a categorical array, and use the iscategory function to test the categories of a categorical array.
Modifying Categorical Arrays
1. Adding Categories
[Example 2-63] Use the addcats function to add categories to a categorical array. You can add them to the end of the categorical array, or specify a position and add the category before or after that position.
Enter in the Command Window:
A=categorical({'大','小','中';'小','大','小'});
categories(A)
ans =
'中'
'大'
'小'
B=addcats(A, {'很大','微小'});
categories(B)
ans =
'中'
'大'
'小'
'很大'
'微小'
[Example 2-64] To demonstrate inserting a category at a specified position, first create an ordinal categorical array.
Enter in the Command Window:
A = categorical({'m' 'l'; 's' 'xl'; 'l' 'm'},{'s' 'm' 'l' 'xl'},'Ordinal',true)
A =
m l
s xl
l m
A is an ordinal categorical array with the specified order s < m < l < xl. Add category xs before category s below.
B=addcats(A, 'xs','Before','s');
categories(B)
ans =
'xs'
's'
'm'
'l'
'xl'
2. Setting New Categories
[Example 2-65] Use the setcats function to set new categories for a categorical array.
For the categorical array A below,
A=categorical({'日用品','生鲜','熟食';'熟食','日用品','生鲜'});
A =
日用品 生鲜 熟食
熟食 日用品 生鲜
Reset new categories.
B=setcats(A, {'日用品','生鲜','粮油'})
B =
日用品 生鲜 <undefined>
<undefined> 日用品 生鲜
Elements in the original categorical array A that are not in the newly specified categories are represented by <undefined>. You can assign values directly, such as:
B(1,3)= '粮油';
B(2,1)= '粮油';
B(2,1)= '粮油';
B
B =
日用品 生鲜 粮油
粮油 日用品 生鲜
3. Merging Categories
[Example 2-66] Use the mergecats function to merge categories. You can also specify multiple categories to merge, merging them into the first category, or specify a new name for the merged category.
Enter in the Command Window:
A = categorical({'red';'blue';'pink';'red';'blue';'pink'});
Merge categories 'red' and 'pink' in categorical array A into 'red'.
B=mergecats(A,{ 'red','pink'})
B =
red
blue
red
red
blue
red
You can also specify a new name for the merged category.
B=mergecats(A,{ 'red','pink'}, '红色')
B =
红色
blue
红色
红色
blue
红色
4. Removing Categories
[Example 2-67] Use the removecats function to remove unused categories or specified categories.
For the categorical array A below, 3 categories are defined, but only two categories are used in the array elements.
A = categorical({'red';'blue';'blue';'red';'blue';'red'},{'red','blue';'pink'});
categories(A)
ans =
'red'
'blue'
'pink'
Use the removecats function to remove the unused 'pink' category.
B=removecats(A)
ans =
'red'
'blue'
You can also specify the 'pink' category to remove.
B=removecats(A, 'pink');