Control Implicit Expansion in Generated Code
R2026bWhen MATLAB® performs element-wise operations, it implicitly expands arrays with compatible sizes to produce arrays of the same size. By default, the generated code also uses implicit expansion. However, you can disable implicit expansion globally, for specific functions, and for specific operations.
For more information about the effects of implicit expansion on the size and performance of the generated code, see Effects of Implicit Expansion on Generated Code.
Disable Implicit Expansion Globally
To disable implicit expansion globally, use one of these approaches:
In the Code Generation Settings dialog box, clear the Enable implicit expansion check box.
In a code configuration object, set the
EnableImplicitExpansionproperty tofalse.In Simulink®, clear the Enable implicit expansion in MATLAB functions (Simulink) model configuration parameter.
Note
If your project includes user-written or MathWorks® functions that rely on implicit expansion, disabling implicit expansion globally can cause code generation errors.
Disable Implicit Expansion for Individual Functions
To disable implicit expansion for specific functions, call the coder.noImplicitExpansionInFunction directive in the function. For example, this
code uses coder.noImplicitExpansionInFunction to prevent implicit expansion for all
operations in the function vector_sum.
function out = vector_sum(a,b) coder.noImplicitExpansionInFunction out = a+b; end
The coder.noImplicitExpansionInFunction directive does not disable implicit
expansion in your MATLAB code. It disables implicit expansion only in the generated code.
Disable Implicit Expansion for Individual Operations
To disable implicit expansion for operands of the same size in a specific binary
operation, use the function coder.sameSizeBinaryOp. For example:
function out = vector_sum(a,b) out = coder.sameSizeBinaryOp(@plus,a,b); end
The coder.sameSizeBinaryOp function prevents implicit expansion in MATLAB and in generated code. If you pass operands of different sizes to coder.sameSizeBinaryOp,
MATLAB produces an error during execution. If the operand sizes conflict at code
generation time, code generation fails. If the operands have variable-size dimensions and code
generation succeeds, the generated code produces an error when the operand sizes conflict at
run time.
See Also
codegen | coder.noImplicitExpansionInFunction | coder.sameSizeBinaryOp