Main Content

Dimension Preservation of Multidimensional Arrays

Dimension Preservation in Generated Code

The code generator produces one-dimensional arrays in the C/C++ code for multidimensional model data. For example, consider matrix matrixParam.

matrixParam = 
        1        2        3
        4        5        6
By default, MATLAB® stores data in column-major format. The code generator defines matrix matrixParam in the generated code as:
/* const_params.c */
matrixParam[6] = {1, 4, 2, 5, 3, 6};
The code generator initializes matrix matrixParam in the generated code as:
/* model.c */
extern const real_T matrixParam[6];

for(int i = 0; i < 6; i++) {
    ... = matrixParamValue[i];
}

When you set the model configuration parameter Array layout to Row-major, you can preserve dimensions of multidimensional arrays in the generated code. Preserving array dimensions in generated code enhances integration with external code.

For example, in row-major array layout, the code generator preserves the array dimensions in type definition in the generated code as:

/* const_params.c */
const real_T matrixParam[2][3] = {{1, 2, 3}, {4, 5, 6}};
The code for initializing the multidimensional data is nested as:
/* model.c */
extern const real_T matrixParam[2][3];

for(int i = 0; i < 2; i++) {
    for (int j = 0; j < 3; j++) {
         ... = matrixParam[i][j];
    }
}

Difference in Dimension Preservation Between MATLAB and C

When you define a multidimensional array in the MATLAB Command Window, MATLAB stores the array in column-major format. For example:

>> matrixParam.Value = [1 2 3; 4 5 6];  % Data inilitialized in row-major

>> matrixParam.Value                    % Data displayed in row-major
ans =
     1     2     3
     4     5     6

>> matrixParam.Value(:)'                % Data stored in column-major
ans =
     1     4     2     5     3     6
Even if the data is initialized in row-major format in the MATLAB Command Window, MATLAB serializes data in memory in column-major format.

When the code generator preserves dimensions, the generated code is:

real_T matrixParam[2][3] = { { 1.0, 2.0, 3.0 }, { 4.0, 5.0, 6.0 } } ;
For 2-D arrays, the index order and data definition are consistent with what you see in the MATLAB Command Window and the generated code.

For n-D arrays, when the code generator produces row-major code and preserves array dimensions, the index order is consistent with MATLAB. The data definition of n-D arrays in the generated code is not consistent with the data displayed in the MATLAB Command Window. For example, consider that matrixParam is a Simulink.Parameter:

>> matrixParam.Value = reshape(1:24,[4 3 2])      

>> matrixParam.Value                              
ans(:,:,1) =
     1     5     9
     2     6    10
     3     7    11
     4     8    12
ans(:,:,2) =
    13    17    21
    14    18    22
    15    19    23
    16    20    24

>> matrixParam.Value(:)'
ans =

  Columns 1 through 10

     1     2     3     4     5     6     7     8     9    10

  Columns 11 through 20

    11    12    13    14    15    16    17    18    19    20

  Columns 21 through 24

    21    22    23    24

In row-major array layout, the code generator preserves the array dimensions during type definition in the generated code as:

/* const_params.c */
const real_T matrixParam[4][3][2] = { { { 1.0, 13.0 }, { 5.0, 17.0 }, { 9.0, 21.0 } },
  { { 2.0, 14.0 }, { 6.0, 18.0 }, { 10.0, 22.0 } }, { { 3.0, 15.0 }, { 7.0, 19.0
    }, { 11.0, 23.0 } }, { { 4.0, 16.0 }, { 8.0, 20.0 }, { 12.0, 24.0 } } };
The code for initializing the multidimensional data is nested as:
/* model.c */
extern const real_T matrixParam[4][3][2];

for (i = 0; i < 3; i++) {
    for (i_1 = 0; i_1 < 4; i_1++) {
      for (i_0 = 0; i_0 < 2; i_0++) {
        ... = ... + matrixParam[i_1][i][i_0];
      }
    }
  }

Related Topics