Combine Categorical Arrays
This example shows how to combine categorical arrays.
Create Categorical Arrays
Create a categorical array that contains the preferred lunchtime beverage of 25 students in classroom A.
rng("default") A = randi(3,[25,1]); A = categorical(A,1:3,["milk" "water" "juice"])
A = 25×1 categorical
juice
juice
milk
juice
water
milk
milk
water
juice
juice
milk
juice
juice
water
juice
milk
water
juice
juice
juice
water
milk
juice
juice
juice
Summarize the categorical array.
summary(A)
A: 25×1 categorical
milk 6
water 5
juice 14
<undefined> 0
Create another categorical array that contains the preferences of 28 students in classroom B.
B = randi(3,[28,1]); B = categorical(B,1:3,["milk" "water" "juice"])
B = 28×1 categorical
juice
juice
water
water
milk
juice
milk
milk
milk
milk
juice
juice
milk
juice
milk
water
water
juice
juice
milk
water
water
water
juice
juice
milk
juice
water
Summarize the categorical array.
summary(B)
B: 28×1 categorical
milk 9
water 8
juice 11
<undefined> 0
Concatenate Categorical Arrays
Concatenate the data from classrooms A and B into a single categorical array, Group1.
Group1 = [A;B]
Group1 = 53×1 categorical
juice
juice
milk
juice
water
milk
milk
water
juice
juice
milk
juice
juice
water
juice
milk
water
juice
juice
juice
water
milk
juice
juice
juice
juice
juice
water
water
milk
⋮
Summarize the categorical array, Group1.
summary(Group1)
Group1: 53×1 categorical
milk 15
water 13
juice 25
<undefined> 0
Create Categorical Array with Different Categories
Create a categorical array, Group2, that contains data from 50 students who were given the additional beverage option of soda.
Group2 = randi(4,[50,1]); Group2 = categorical(Group2,1:4,["juice" "milk" "soda" "water"])
Group2 = 50×1 categorical
juice
juice
milk
water
milk
soda
juice
water
milk
soda
soda
water
water
soda
juice
juice
milk
water
milk
water
juice
water
milk
juice
milk
soda
milk
milk
water
soda
⋮
Summarize the categorical array.
summary(Group2)
Group2: 50×1 categorical
juice 12
milk 14
soda 10
water 14
<undefined> 0
Concatenate Arrays with Different Categories
Concatenate the data from Group1 and Group2.
students = [Group1;Group2]
students = 103×1 categorical
juice
juice
milk
juice
water
milk
milk
water
juice
juice
milk
juice
juice
water
juice
milk
water
juice
juice
juice
water
milk
juice
juice
juice
juice
juice
water
water
milk
⋮
Summarize the resulting categorical array. Concatenation appends the categories exclusive to the second input, soda, to the end of the list of categories from the first input, milk, water, juice, soda.
summary(students)
students: 103×1 categorical
milk 29
water 27
juice 37
soda 10
<undefined> 0
To change the order of the categories in the categorical array, use the reordercats function.
students = reordercats(students,["juice" "milk" "water" "soda"]); categories(students)
ans = 4×1 cell
{'juice'}
{'milk' }
{'water'}
{'soda' }
Union of Categorical Arrays
To find the unique responses from Group1 and Group2, use the union function.
C = union(Group1,Group2)
C = 4×1 categorical
milk
water
juice
soda
union returns the combined values from Group1 and Group2 with no repetitions. In this case, C is equivalent to the categories of the concatenation, students.
All of the categorical arrays in this example were nonordinal. To combine ordinal categorical arrays, they must have the same sets of categories including their order.
See Also
categorical | categories | summary | union | cat | horzcat | vertcat