### Diagnosing and Fixing Size Mismatch Errors

Check your code for these issues:

Assigning Variable-Size Matrices to Fixed-Size Matrices

You cannot assign variable-size matrices to fixed-size matrices
in generated code. Consider this example:

Compiling this function produces this error:

??? Dimension 1 is fixed on the left-hand side
but varies on the right ...

There are several ways to fix this error:

Allow matrix `A`

to grow by adding
the `coder.varsize`

construct:

Explicitly restrict the size of matrix `B`

to
3-by-3 by modifying the `assert`

statement:

Use explicit indexing to make `B`

the
same size as `A`

:

Empty Matrix Reshaped to Match Variable-Size Specification

If you assign an empty matrix `[]`

to variable-size
data, MATLAB^{®} might silently reshape the data in generated code
to match a `coder.varsize`

specification. For example:

In this example, `coder.varsize`

defines `Y`

as
a column vector of up to 10 elements, so its first dimension is fixed
at size 1. The statement `Y = []`

designates the
first dimension of `Y`

as 0, creating a mismatch.
The right hand side of the assignment is an empty matrix and the left
hand side is a variable-size vector. In this case, MATLAB reshapes
the empty matrix `Y = []`

in generated code to ```
Y
= zeros(1,0)
```

so it matches the `coder.varsize`

specification.

Performing Binary Operations on Fixed and Variable-Size Operands

You cannot perform binary operations on operands of different
sizes. Operands have different sizes if one has fixed dimensions and
the other has variable dimensions. For example:

When you compile this function, you get an error because `y`

has
fixed dimensions (3 x 3), but `x`

has variable dimensions.
Fix this problem by using explicit indexing to make `x`

the
same size as `y`

: