Main Content

coder.loop.reverse

Reverse loop iteration order in generated code

Since R2023a

    Description

    example

    coder.loop.reverse("loopID") prompts the code generator to reverse the loop iteration order for the for-loop whose index name is loopID in the generated code.

    For more information about loop optimizations, see Optimize Loops in Generated Code.

    coder.loop.reverse prompts the code generator to reverse the loop iteration order for the for loop defined immediately after this function call in the generated code. Use this transform when you know the upper bound of the loop iterator.

    loopObj = coder.loop.reverse(___) returns a loop control object with transformSchedule property set to coder.loop.reverse. Use the apply method to apply the transform to the specified loop.

    Examples

    collapse all

    Use the coder.loop.reverse function to the reverse iteration order of for loops in the generated code.

    Define a MATLAB® function applyReverseTransform that performs an addition operation on an array.

    function out = applyReverseTransform
    out = zeros(1,100);
    
    coder.loop.reverse('i');
    for i = 1:100
        out(i) = out(i) + i;
    end

    Generate code for this function by running the following command:

    codegen applyReverseTransform -config:lib -launchreport

    Inspect the code generated for the function in the code generation report. Notice that the for-loop index in the generated function reduces from 99 to 0. This optimization can be beneficial if the upper-bound of the index is known.

    void applyReverseTransform(double out[100])
    {
      int i;
      memset(&out[0], 0, 100U * sizeof(double));
      for (i = 99; i >= 0; i--) {
        out[i] += (double)i + 1.0;
      }
    }

    Input Arguments

    collapse all

    for-loop identifier or index name to reverse, specified as a character vector a string scalar.

    Data Types: char | string

    Output Arguments

    collapse all

    Loop control object with transformSchedule property set to coder.loop.reverse.

    Version History

    Introduced in R2023a