Navigate to a folder containing sample data.

cd(matlabroot)
cd('help/toolbox/stats/examples')

Load the sample data.

load shift

The data shows the deviations from the target quality characteristic
measured from the products that 5 operators manufacture during three
different shifts, morning, evening, and night. This is a randomized
block design, where the operators are the blocks. The experiment is
designed to study the impact of the time of shift on the performance.
The performance measure is the deviation of the quality characteristics
from the target value. This is simulated data.

`Shift` and `Operator` are
nominal variables.

shift.Shift = nominal(shift.Shift);
shift.Operator = nominal(shift.Operator);

Fit a linear mixed-effects model with a random intercept
grouped by operator to assess if performance significantly differs
according to the time of the shift.

lme = fitlme(shift,'QCDev ~ Shift + (1|Operator)');

Display the fixed-effects design matrix.

designMatrix(lme)

ans =
1 1 0
1 0 0
1 0 1
1 1 0
1 0 0
1 0 1
1 1 0
1 0 0
1 0 1
1 1 0
1 0 0
1 0 1
1 1 0
1 0 0
1 0 1

The column of 1s represents the constant term in the model. `fitlme` takes
the evening shift as the reference group and creates two dummy variables
to represent the morning and night shifts, respectively.

Display the random-effects design matrix.

designMatrix(lme,'random')

ans =
(1,1) 1
(2,1) 1
(3,1) 1
(4,2) 1
(5,2) 1
(6,2) 1
(7,3) 1
(8,3) 1
(9,3) 1
(10,4) 1
(11,4) 1
(12,4) 1
(13,5) 1
(14,5) 1
(15,5) 1

The first number, *i*, in the (*i*,*j*)
indices corresponds to the observation number, and *j* corresponds
to the level of the grouping variable, `Operator`,
i.e., the operator number.

Show the full display of the random-effects design matrix.

full(designMatrix(lme,'random'))

ans =
1 0 0 0 0
1 0 0 0 0
1 0 0 0 0
0 1 0 0 0
0 1 0 0 0
0 1 0 0 0
0 0 1 0 0
0 0 1 0 0
0 0 1 0 0
0 0 0 1 0
0 0 0 1 0
0 0 0 1 0
0 0 0 0 1
0 0 0 0 1
0 0 0 0 1

Each column corresponds to a level of the grouping variable, `Operator`.