Main Content

mp_0046: Usage of expressions in array indices

Since R2024b

Guideline Publication

Control Algorithm Modeling Guidelines - Using MATLAB®, Simulink®, and Stateflow®

Sub ID Recommendations

  • NA-MAAB — a

  • JMAAB — a

MATLAB Versions

All

Rule

Sub ID a

Array indices shall not be used to calculate array numbers. However, arithmetic operations with end and the colon operator are exceptions.

Example — Correct
foo = rand(1, 10);
k = 1;
m = 2 * k - 1;
foo(m) = 1;
foo(end - 1) = 2;
Example — Incorrect
foo = rand(1, 10);
k = 1;
foo(2 * k - 1) = 1;

Rationale

Sub ID a:

  • Calculating values before using them as indices to access array elements reduces readability.

Verification

Model Advisor check: Check array indices (Simulink Check)

Last Changed

R2024b

Version History

Introduced in R2024b