Main Content

coder.loop.reverse

Reverse loop iteration order in generated code

Since R2023a

    Description

    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.

    example

    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 reverse the iteration order of for-loops in the generated code. Use the coder.loop.reverse function to optimize the generated code.

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

    type applyReverseTransform.m
    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 at the command line.

    codegen applyReverseTransform -config:lib
    Warning: Code generation is using a coder.EmbeddedCodeConfig object. Because
    Embedded Coder is not installed, this might cause some Embedded Coder features
    to fail.
    
    Code generation successful (with warnings): To view the report, open('codegen/lib/applyReverseTransform/html/report.mldatx')
    

    Inspect the code generated for this function. 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.

    type(fullfile("codegen","lib","applyReverseTransform","applyReverseTransform.c"))
    /*
     * File: applyReverseTransform.c
     *
     * MATLAB Coder version            : 24.2
     * C/C++ source code generated on  : 05-Sep-2024 16:34:53
     */
    
    /* Include Files */
    #include "applyReverseTransform.h"
    #include <string.h>
    
    /* Function Definitions */
    /*
     * Arguments    : double out[100]
     * Return Type  : void
     */
    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;
      }
    }
    
    /*
     * File trailer for applyReverseTransform.c
     *
     * [EOF]
     */
    

    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