# Applying a Scalar Algorithm to a Vector

This week, Seth Popinchalk is visiting MathWorks Japan. Today's post is from Guy Rouleau:

One of my favorite situations in technical support goes like this: Someone asks "Is it possible to do something?" and I can answer "Yes, this has been implemented in the latest release." This happened this week when someone asked about applying a scalar algorithm to a vector signal, and I pointed them to the For Each Subsystem documentation.

I implemented a subsystem using the J-K Flip-Flop block. Now I would like to use this subsystem on a vector of signals. How can I do this?

Many blocks in Simulink support Scalar Expansion, but some, like the J-K Flip-Flop, do not. Let's look for example at the Simulink demo titled Modulo-4 Counter Using Flip-Flops.

In this demo, two J-K Flip-Flop blocks are used to implement a Modulo 4 counter. Combined with the D Latch and a few Logical Operators, this system generates a half clock cycle width pulse on every fourth clock pulses.

Before R2010a, the solution to vectorize this system was to separate the individual components of the vector and copy the subsystem multiple times. For small vectors it is not too bad, but for a vector of 1000 elements, it definitely becomes a problem.

In MATLAB R2010a, it is now possible to place the blocks to be expanded inside the For Each Subsystem and connect the vector directly to it.

The question I receive the most often about the For Each Subsystem is: How is it different from the For Iterator Subsystem. In my opinion, the main advantage is that inside the For Each Subsystem, each block that has states maintains separate sets of states for each element or subarray that it processes. The automatic decomposition and concatenation of the input and output signals also saves a lot of time.

Now it's you turn

Do you see how the For Each Subsystem could be useful in your application?

|