Documentation

coder.columnMajor

Specify column-major array layout for a function or class

Description

example

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

Examples

collapse all

Specify column-major array layout for a function by insertingcoder.columnMajorinto the function body.

Suppose thatmyFunctionis 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.

functionS = myFunction(A,B)%#codegen% check to make sure inputs are validifsize(A,1) ~= size(B,1) || size(A,2) ~= size(B,2) disp('Matrices must be same size.')return;end% make both matrices symmetricB = B*B'; A = A*A';% add matricesS = addMatrix(A,B);end

Write a function for matrix addition calledaddMatrix. Specify column-major foraddMatrixby usingcoder.columnMajor.

functionS = addMatrix(A,B)%#codegenS = zeros(size(A)); coder.columnMajor;% c指定olumn-major array layoutS = A + B;end

Generate code formyFunction. Use thecodegencommand.

codegenmyFunction-args{ones(10,20),ones(10,20)}-config:lib-launchreport-rowmajor

Because of thecodegen -rowmajoroption, the matrix multiplication inmyFunctionuses row-major layout. However, the generated code foraddMatrixuses column-major array layout due to thecoder.columnMajorcall.

Tips

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

  • The specification of array layout inside a function supersedes the array layout specified with thecodegencommand. For example, if the functionfoocontainscoder.columnMajor, and you generate code by using:

    codegenfoo-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 distinctcoder.rowMajorcall, 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.columnMajoris ignored outside of code generation and simulation.

Introduced in R2018a