Specify row-major array layout for a function or class



coder.rowMajor specifies row-major array layout for the data used by the current function in generated code. When placed in a class constructor, coder.rowMajor specifies row-major layout for data used by the class.


By default, code generation uses column-major array layout.


collapse all

Specify row-major array layout for a function by inserting coder.rowMajor into the function body.

Suppose that myFunction is the top-level function of your code. Your application requires you to perform matrix addition with row-major array layout and matrix multiplication with column-major layout.

function S = myFunction(A,B)
% check to make sure inputs are valid
if size(A,1) ~= size(B,1) || size(A,2) ~= size(B,2)
    disp('Matrices must be same size.');
% make both matrices symmetric
B = B*B';
A = A*A';
% add matrices
S = addMatrix(A,B);

Write a function for matrix addition called addMatrix. Specify row-major layout for addMatrix by using coder.rowMajor.

function S = addMatrix(A,B) 
S = zeros(size(A));
coder.rowMajor; % specify row-major array layout
S = A + B;

Generate code for myFunction. Use the codegen command.

codegen myFunction -args {ones(10,20),ones(10,20)} -config:lib -launchreport

The code generator produces code for addMatrix that uses row-major array layout. However, the matrix multiplication from the top-level function uses the default layout, column-major.


  • To specify row-major array layout for all the functions in your generated code, use the codegen -rowmajor option.

  • Other functions called from within a row-major function inherit the row-major specification. However, if one of the called functions has its own distinct coder.columnMajor call, the code generator changes the array layout accordingly. If a row-major function and a column-major function call the same function, which does not have its own array layout specification, the code generator produces a row-major version and column-major version of the function.

  • coder.rowMajor is ignored outside of code generation and simulation.

Introduced in R2018a