%% Vectorizing a Scalar Algorithm with a For Each Subsystem
% This example shows how to use the For Each subsystem. 
% In this example the operations are performed on a vector for simplicity. 

% Copyright 2009-2012 The MathWorks, Inc.

%%
% Use the *Open Model* button to open the example model
% |sldemo_foreach_vectorized|. This model contains a For Each subsystem
% that processes the input signals one by one.
%
% The inputs to the For Each subsystem are the signals to process,
% the coefficients of the FIR filter to use with each of these
% signals, and the common gain.
%
% Each signal is scalar and needs to be processed individually.
% Consequently, the For Each block that controls the partition dimension is
% set to partition the input signal along dimension 1, by slicing through a
% partition width of 1. For each input signal, a corresponding set of
% coefficients must also be partitioned using the same criteria. The gain
% is common to all the signals, so this input is not partitioned.
% 
% Since the output signal dimension is expected to match the input signal
% dimension, the concatenation dimension is set equal to the partition
% dimension. If you prefer to change the signal dimension (a transpose in
% this case), you can choose 2 as the concatenation dimension.
%
% For the For Each subsystem, you must set the partition dimension and
% width, but not the input signal size. A check then ensures that this size
% is a multiple of the partition width. If no error is detected, the
% subsystem then calculates the number of independent executions that the
% enclosed subsystem will perform. These executions are independent, in
% that the state associated with a given block contained in the subsystem
% has a unique value in each of the respective executions.
%
% To see a non-vectorized implementation for this model, double-click on
% the block in the lower-right corner of the model. This implementation
% does not use the For Each subsystem, but mimics its functionality by
% replicating the subsystem as many times as necessary and also by
% selecting and concatenating the signals appropriately. This replication
% process is prone to error and is not scalable -- changing the input
% signal size would require changing the model accordingly.

model = 'sldemo_foreach_vectorized';
open_system(model);
evalc('sim(model)');
