%% Apply Operations and Functions Without Implicit Expansion
% Use coder.sameSizeBinaryOp to apply binary operations and functions where implicit expansion is not required.
% Using coder.sameSizeBinaryOp ensures that any variable-size operands of compatible sizes are not expanded automatically. The generated code does not include additional code to enable the automatic expansion of the operands.
% In this example, the plus function applies the operation with implicit expansion. The coder.sameSizeBinaryOp function applies the operation without implicit expansion.

type addExample.m

% Define the input types.

a_type = coder.typeof(1,[5 1]);
b_type = coder.typeof(1,[5 inf]);

%
% Generate code for the function addExample by using this command.

codegen addExample -args {a_type, b_type} -config:lib -report

%% Compare Output Sizes
% In the code generation report created in the previous step, place your cursor over the two operations.
%
% The size of the output from the  plus operation is 5x:? whereas the size of the output of coder.sameSizeBinaryOp is 5x1.
% The plus function implicitly expands its operands to the output size.
% The coder.sameSizeBinaryOp function disables implicit expansion to prevent the automatic expansion of the operands.
%% Examine the Generated Code

type codegen/lib/addExample/addExample.c

% The code generated to calculate out1 by using coder.sameSizeBinaryOp is seen in the first for loop in the function addExample. The code generated to calculate out2 is seen below the first for loop. The generated code for out1 is much smaller, as compared to the code generated to calculate out2.
% The code generated for the plus function needs additional code to expand its operands.
% Copyright 2020 The MathWorks, Inc.