Main Content


Specify column-major array layout for a function or class



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


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


collapse all

Specify column-major array layout for a function by inserting coder.columnMajor into the function body.

Suppose that myFunction is the top-level function of your code. Your application requires you to perform matrix addition with column-major array layout and matrix multiplication with row-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 column-major for addMatrix by using coder.columnMajor.

function S = addMatrix(A,B) 
S = zeros(size(A));
coder.columnMajor; % specify column-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 -rowmajor

Because of the codegen -rowmajor option, the matrix multiplication in myFunction uses row-major layout. However, the generated code for addMatrix uses column-major array layout due to the coder.columnMajor call.


  • The code generator uses column-major array layout by default.

  • The specification of array layout inside a function supersedes the array layout specified with the codegen command. For example, if the function foo contains coder.columnMajor, and you generate code by using:

    codegen foo -rowmajor

    then the generated code still uses column-major layout.

  • Other functions called from within a column-major function inherit the column-major specification. However, if one of the called functions has its own distinct coder.rowMajor 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.columnMajor is ignored outside of code generation and simulation.

Extended Capabilities

C/C++ Code Generation
Generate C and C++ code using MATLAB® Coder™.

GPU Code Generation
Generate CUDA® code for NVIDIA® GPUs using GPU Coder™.

Version History

Introduced in R2018a