Accelerating the pace of engineering and science

# Documentation Center

• Trials

## Combine Categorical Arrays

This example shows how to combine two categorical arrays.

Create Categorical Arrays

Create a categorical array, A, containing the preferred lunchtime beverage of 25 students in classroom A.

```A = gallery('integerdata',3,[25,1],1);
A = categorical(A,1:3,{'milk' 'water' 'juice'});
```

A is a 25-by-1 categorical array with three distinct categories: milk, water, and juice.

Summarize the categorical array, A.

```summary(A)
```
```     milk       8
water      8
juice      9

```

Eight students in classroom A prefer milk, eight prefer water, and nine prefer juice.

Create another categorical array, B, containing the preferences of 28 students in classroom B.

```B = gallery('integerdata',3,[28,1],3);
B = categorical(B,1:3,{'milk' 'water' 'juice'});
```

B is a 28-by-1 categorical array containing the same categories as A.

Summarize the categorical array, B.

```summary(B)
```
```     milk       12
water      10
juice       6

```

Twelve students in classroom B prefer milk, ten prefer water, and six prefer juice.

Concatenate Categorical Arrays

Concatenate the data from classrooms A and B into a single categorical array, Group1.

```Group1 = [A;B];
```

Summarize the categorical array, Group1

```summary(Group1)
```
```     milk       20
water      18
juice      15

```

Group1 is a 53-by-1 categorical array with three categories: milk, water, and juice.

Create Categorical Array with Different Categories

Create a categorical array, Group2, containing data from 50 students who were given the additional beverage option of soda.

```Group2 = gallery('integerdata',4,[50,1],2);
Group2 = categorical(Group2,1:4,{'juice' 'milk' 'soda' 'water'});
```

Summarize the categorical array, Group2.

```summary(Group2)
```
```     juice      18
milk       10
soda       13
water       9

```

Group2 is a 50-by-1 categorical array with four categories: juice, milk, soda, and water.

Concatenate Arrays with Different Categories

Concatenate the data from Group1 and Group2.

```students = [Group1;Group2];
```

Summarize the resulting categorical array, students.

```summary(students)
```
```     milk       30
water      27
juice      33
soda       13

```

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.

Use reordercats to change the order of the categories in the categorical array, students.

```students = reordercats(students,{'juice','milk','water','soda'});

categories(students)
```
```ans =

'juice'
'milk'
'water'
'soda'

```

Union of Categorical Arrays

Use the function union to find the unique responses from Group1 and Group2.

```C = union(Group1,Group2)
```
```C =

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.