Main Content

Resolve Error: Size Mismatches


The code generator produces size mismatch errors when array sizes are incompatible or implicit expansion is unavailable.

Most binary operators and functions in MATLAB® and generated code support numeric arrays that have compatible sizes. Two inputs have compatible sizes if, for every dimension, the sizes of the inputs are either the same or one of them is 1. In the simplest cases, two array sizes are compatible if they are exactly the same or if one is a scalar. For example:

magic(4) + ones(4,1);
% where magic(4) =             ones(4,1)  = 
%   16     2     3    13                    1
%    5    11    10     8                    1
%    9     7     6    12                    1
%    4    14    15     1                    1
ans =

    17     3     4    14
     6    12    11     9
    10     8     7    13
     5    15    16     2

The second array implicitly expands to match the dimensions of the first matrix. For more information, see Compatible Array Sizes for Basic Operations.

Implicit expansion might be unavailable while performing binary operations on arrays of compatible size if any the following conditions are true :

Size mismatches or unavailability of implicit expansion generates the following error:

%Size mismatch between two arrays
Size mismatch (size [10][1] ~= size [1][10])

When the above conditions are true for structure fields and cell elements, the code generator produces the following errors respectively:

%Size mismatch in structure fields
Size mismatch (size [10][1] ~= size [1][10]) in field StructField

%Size mismatch in cell elements
Size mismatch (size [10][1] ~= size [1][10]) in element cellElement.

Possible Solutions

Verify that, in binary operations where you enable implicit expansion, the operations are in the scope of functions. Check for these conditions:

  • Array size compatibility.

  • Binary operations in the scope of functions that call coder.noImplicitExpansionInFunction.

  • coder.sameSizeBinaryOp does not implicitly expand its operands or support scalar expansion.

  • If you have turned off implicit expansion for the whole project, all operations that require implicit expansion generate an error.

Perform Binary Operations on Arrays of Compatible Sizes

If you must carry out a binary operation on arrays of differing sizes, make sure that sizes are compatible and implicit expansion is enabled in the function scope. See Compatible Array Sizes for Basic Operations.

Call Binary Operation Without coder.noImplicitExpansionInFunction

If you must include coder.noImplicitExpansionInFunction in your function, call the required binary operation in another function where implicit expansion is enabled.

Call Binary Operation Without coder.sameSizeBinaryOp

If you do not want implicit expansion for a specific operation, provide input arguments that are of same size to coder.sameSizeBinaryOp. coder.sameSizeBinaryOp does not allow scalar expansion and generates an error if the input arguments are not of the same size.

Enable Implicit Expansion for Project

If enabling implicit expansion does not affect your project, consider enabling it by setting the EnableImplicitExpansion property in your code configuration object to true.

If you need implicit expansion for specific operations, consider using coder.sameSizeBinaryOp or coder.noImplicitExpansionInFunction to prevent the other operations from implicitly expanding. See Optimize Implicit Expansion in Generated Code.

See Also


Related Topics