%% Disable Implicit Expansion Within a Function
% Use coder.noImplicitExpansionInFunction to disable implicit expansion in the code generated for a MATLAB function by calling it in within the required function. Disable implicit expansion to apply binary operations and functions without:
% Automatic size change of compatible size operands
% Additional code generation
% Performance variations
% In this example, the functions DisableImpExpinFunction and ImpExpinFunction add two operands of compatible sizes with identical code but the former disables implicit expansion. The code generated for DisableImpExpinFunction does not contain additional code to apply implicit expansion. The code generated for ImpExpinFunction contains loops that must be executed if the operands are of compatible size and can be implicitly expanded at run-time.
% In the code generation report for the two functions, the sizes of the expression assigning the out values are different.
% Define a function DisableImpExpinFunction that calculates the addition of two operands without implicit expansion by calling coder.noImplicitExpansionInFunction within the required function.

type DisableImpExpinFunction.m

%
% Define a function ImpExpinFuntion that calculates the addition of two operands with implicit expansion enabled.

type ImpExpinFunction.m

%
% Define a fixed-size and variable-size input type for these functions.

a = coder.typeof(1,[2 1]);
b = coder.typeof(1,[2 inf]);

% Generate code for the functions by using these commands:

codegen DisableImpExpinFunction -args {a,b} -config:lib -lang:c++ -report
codegen ImpExpinFunction.m -args {a,b} -config:lib -lang:c++ -report

%
%% Compare Generated Code for Functions
% To apply implicit expansion, the generated code introduces additional code to automatically expand the operands. The difference between the generated code for the two functions is shown in this code.

type codegen/lib/DisableImpExpinFunction/DisableImpExpinFunction.cpp
type codegen/lib/ImpExpinFunction/ImpExpinFunction.cpp

% When implicit expansion is enabled, the generated code includes additional code to change the size of the operands, as seen in ImpExpinFunction. If implicit expansion is disabled, the generated code does not include additional code to apply any size changes to the operands, as seen in DisableImpExpinFunction.
% Compare the size of the output variables in the code generation report. The expression assigning the out variable in DisableImpExpinFunction is of the size 2x1.
% The expression assigning the out variable in ImpExpinFunction is of the size 2x:?.
% Copyright 2020 The MathWorks, Inc.