Main Content

For Each

Process elements or subarrays of an input signal or mask parameter independently

  • Library:
  • Ports & Subsystems

  • For Each block

Description

The For Each block serves as a control block for the For Each Subsystem block. Specifically, the For Each block enables the blocks inside the For Each subsystem to process the elements of either input signals or mask parameters independently. Each block inside this subsystem that has states maintains a separate set of states for each element or subarray that it processes. As the set of blocks in the subsystem processes the elements or subarrays, the subsystem concatenates the results to form output signals.

Use a For Each subsystem to iteratively compute outputs after changing inputs or mask parameters. To do so, configure the partitioning of input signals or mask parameters in the For Each block dialog box.

Partition Input Signals to the Subsystem

To specify which input signals to partition for each iteration in a For Each subsystem, use the Input Partition tab in the dialog box of the For Each block. When specifying a signal to be partitioned, specify the Partition Dimension, Partition Width, and Partition Offset parameters.

Partition Mask Parameters of the Subsystem

You can partition the mask parameters of a For Each Subsystem block. Partitioning is useful for systems that have identical structures in each iteration but different parameter values. In this case, changing the model to partition extra input signals for each parameter is cumbersome. Instead, add a mask parameter to a For Each subsystem. For more information, see Create a Simple Mask. To select the mask parameter for partitioning, use the Parameter Partition tab of the For Each block dialog box. For more information, see Select Partition Parameters, below.

Concatenate Output

Define the dimension along which to concatenate the results by specifying the Concatenation Dimension in the Output Concatenation tab.

The results generated by the block for each subarray are stacked along the concatenation dimension. By default, dimension 1 (y-axis) is used, meaning that the results are stacked vertically. However, if you specify a concatenation dimension of 2, the results concatenate along the horizontal direction (x-axis). Thus, if the process generates row vectors, then the concatenated result is a matrix in the first case and a row vector in the second case.

Select Partition Parameters

When selecting an input signal or subsystem mask parameter for partitioning, you need to specify how to decompose it into elements or subarrays for each iteration. Do this by setting integer values for the Partition Dimension, Partition Width, and Partition Offset parameters.

As an illustration, consider an input signal matrix A of the form:

The labels d1 and d2, respectively, define dimensions 1 and 2. If you retain the default setting of 1 for both the partition dimension and the partition width, and 0 for the partition offset, then Simulink® slices perpendicular to partition dimension 1 at a width equal to the partition width, that is one element:

Matrix A decomposes into these three row vectors:

If instead you specify 2 as the partition dimension, Simulink slices perpendicular to dimension 2 to form three column vectors:

In addition to setting the Partition Dimension to 2, if you set the Partition Width to 2 and the Partition Offset to -1, Simulink uses two overlapping 3-by-2 partitions for processing.

For an example using the Partition Offset parameter, open the model slexForEachOverlapExample.

By default, all partitions of the input signal or mask parameter are processed. To process a subset of the partitions, enter the number of partitions to process as the Number of iterations. In the matrix examples above, if Partition Offset is set to 0 (the default) and Number of iterations is set to 2, only the first 2 rows or columns of the input matrix A are processed.

Note

Only signals are considered one-dimensional in Simulink. Mask parameters are row or column vectors, according to their orientation. To partition a row vector, specify the partition dimension as 2 (along the columns). To partition a column vector, specify the partition dimension as 1 (along the rows).

Ports

Output

expand all

Index of current partition (starting at zero), returned as a scalar.

Dependency

Select Show partition index output port to display this port.

Data Types: int32 | double

Parameters

expand all

Input Partition Tab

Select each input signal you want to partition and specify the corresponding Partition Dimension, Partition Width, and Partition offset parameters. See the Inport block reference page for more information.

List of input ports connected to the For Each Subsystem block.

Select input port signals connected to the For Each Subsystem block to partition into subarrays or elements.

Dependency

Selecting this parameter enables the Partition Dimension, Partition Width, and Partition offset parameters for the selected input port signal.

Programmatic Use

Block Parameter: InputPartition
Type: character vector
Values: 'off' | 'on'
Default: 'off'

Specify the dimension through which to slice the input signal array. The resulting slices are perpendicular to the dimension that you specify. The slices partition the array into subarrays or elements, as appropriate.

1

Specify a dimension of 1.

integer

Specify dimension. Minimum value 1.

Programmatic Use

Block Parameter: InputPartitionDimension
Type: character vector
Values: '1' | '<integer>'
Default: '1'

Specify the width of each partition slice of the input signal.

1

Width of one element.

integer

Specify width. Minimum value 1.

Programmatic Use

Block Parameter: InputPartitionWidth
Type: character vector
Values: '1' | '<integer>'
Default: '1'

Specify the offset for each partition slice of the input signal.

0

No offset between partition slices.

integer

Specify partition offset, where the sum of the partition width and the partition offset is a positive integer.

For example, a Partition Width of 3 and a Partition Offset of -2 indicates that each 3-element slice overlaps its neighboring slices by 2 elements; whereas a Partition Width of 2 and a Partition Offset of 1 indicates that there is a 1-element-wide gap between each pair of neighboring 2-element-wide slices.

Programmatic Use

Block Parameter: InputPartitionOffset
Type: character vector
Values: '0' | '<integer>'
Default: '0'

Parameter Partition Tab

Select each mask parameter to partition and specify the corresponding Partition Dimension and Partition Width parameters. Parameters appear in the list only if you have added an editable parameter to the mask of the parent For Each subsystem.

List of mask parameters for the For Each Subsystem block.

Select mask parameters for the For Each Subsystem block to partition into subarrays or elements.

off

Clear mask parameters.

on

Select mask parameters to partition.

Dependency

Selecting this parameter enables the Partition Dimension and Partition Width parameters for the selected mask parameter.

Programmatic Use

Block Parameter: SubsysMaskParameterPartition
Type: character vector
Values: 'off' | 'on'
Default: 'off'

Specify the dimension through which to slice the mask parameter array. The resulting slices are perpendicular to the dimension that you specify. The slices partition the array into subarrays or elements, as appropriate.

Programmatic Use

Block Parameter: SubsysMaskParameterPartitionDimension
Type: character vector
Values: '1' | '<integer>'
Default: '1'

Specify the width of each partition slice of the mask parameter array.

Programmatic Use

Block Parameter: SubsysMaskParameterPartitionWidth
Type: character vector
Values: '1' | '<integer>'
Default: '1'

Output Concatenation Tab

For each output port, specify the dimension along which to stack (concatenate) the For Each Subsystem block results. See the Outport block reference page for more information.

List of output ports connected to the For Each Subsystem block.

Specify the dimension along which to stack the results of the For Each Subsystem block.

1

The results stack in the d1 direction. If the block generates column vectors, the concatenation process results in a single column vector. If the block generates row vectors, the concatenation process results in a matrix.

2

The results stack in the d2 direction. If the block generates row vectors, the concatenation process results in a single row vector. If the block generates row vectors, the concatenation process results in a matrix.

Programmatic Use

Block Parameter: OutputConcatenationDimension
Type: character vector
Values: '1' | '<integer>'
Default: '1'

Other Parameters

Control display of output port for partition index.

off

Hide output port.

on

Display output port on block.

Dependency

Selecting this parameter enables the Partition index output data type parameter.

Programmatic Use

Block Parameter: ShowIterationIndex
Type: character vector
Values: 'off' | 'on'
Default: 'off'

Specify partition index data type for output port

int32

Select signed 32-bit integer.

double

Select double real.

Programmatic Use

Block Parameter: IterationIndexDataType
Type: character vector
Values: 'int32' | 'double'
Default: 'int32'

To process a subset of data, specify the number of partition slices of the input signal or mask parameter array to process; else specify -1 to process all slices. The number must not be greater than the total number of slices available.

-1

Process all slices

integer

Number of slices to process

For example, a Number of iterations of 3 indicates that only the first 3 slices are to be processed.

Programmatic Use

Block Parameter: SpecifiedNumIters
Type: character vector
Values: '-1' | '<integer>'
Default: '-1'
Introduced in R2010a