Assignment of elements of vector inside for-loop with conditional logics

50 views (last 30 days)
Hi there,
I need to implement in Simulink the following logic illustrated as a simplified pseudo code below. The idea is need to update a vector based on condition inside a for-loop. Some condition may be not perform any operation on the vector elements at all.
Vector = [0, 0, 0];
For iteration=1:3
If iteration<=1
Vector(1) = 1;
elseif iteration<=2
Vector(2) = 3;
else
end
end
I am trying to avoid a memory block to use the whole vector from previous iteration to current iteration because of the computation effort needed to copy the whole vector. The way assignment block works within a for-loop serves this purpose well as desribed here https://www.mathworks.com/help/simulink/slref/iterated-assignment-with-assignment-block.html. However, because of the conditional logic, I am not sure if it is possible.
Appreciate for any suggestions on the implementation

Answers (1)

Umar
Umar about 23 hours ago

Hi @John,

You have a specific condition-based logic that needs to update elements of a vector (Vector = [0, 0, 0]) during each iteration of a loop. The conditions dictate that only certain elements of the vector should be updated based on the current iteration index. The goal is to avoid copying the entire vector for every iteration, which can be computationally expensive. Here are the implementation steps listed below.

1. Set Up the Model: - Open Simulink and create a new model. - Add a For Iterator Subsystem from the Simulink library.

2. Initialize the Vector: - Within the For Iterator Subsystem, initialize your vector using a Constant block or an Inport block with values [0, 0, 0]. This will serve as your initial vector state.

3. Add an Assignment Block: - Drag an Assignment block into your For Iterator Subsystem. This block will allow you to update specific elements of the vector based on the iteration index. - Configure this block to accept two inputs: one for the vector and another for the new value to be assigned.

4. Configure Loop Logic: - Use a Switch block or Multiport Switch block to implement your conditional logic: - Connect the output of the For Iterator to control which element of the vector is updated based on the current iteration. - For iteration <= 1, connect it to update Vector(1). - For iteration <= 2,connect it to update Vector(2). - If none of these conditions are met (i.e., for iteration > 2), simply pass through the current value of that element without making any updates.

5. Connect Outputs: - Ensure that after each update, you connect the output of your Assignment block back into itself or use it as an input for subsequent iterations, allowing it to carry forward any changes made during previous iterations.

6. Final Output: - After exiting the For Iterator loop, connect an Outport block to capture the final state of your vector.

Example Configuration

- In your Assignment block, you might set it up as follows: - Inputs: - First input: Current state of `Vector` - Second input: New value based on conditions - Assignments in your logic:

      if iteration == 1
        Vector(1) = 1;
      elseif iteration == 2
        Vector(2) = 3;
      end

By using this approach, you minimize memory usage since only specific elements are updated rather than copying and reassigning the whole vector.

Hope this helps.

  3 Comments
Umar
Umar about 5 hours ago

Hi @John,

Please see attached screenshot when clicked the link below provided by you in your comments. It mentions “file does not exist”.

https://www.mathworks.com/help/simulink/slref/iterated-assignment-with-assignment-block.html

After going through your comments, to efficiently update specific elements of a vector in Simulink without copying the entire vector, you can utilize the For Iterator Subsystem along with an Assignment block. Here’s a concise breakdown of the process:

Model Setup: Create a new Simulink model and add a For Iterator Subsystem.

Vector Initialization: Use a Constant or Inport block to initialize your vector as [0, 0, 0].

Assignment Block: Insert an Assignment block to update specific vector elements based on the iteration index.

Conditional Logic: Implement a Switch block to determine which vector element to update. For instance, if the iteration index is 1, update Vector(1); if it’s 2, update Vector(2). For iterations greater than 2, simply pass the current value through without modification.

Output Connection: Connect the output of the Assignment block back to the input for subsequent iterations, ensuring that changes persist.

Regarding your questions, "passing through the current value" means that if the condition is not met, the output of the vector element remains unchanged. You do not need a Memory block; simply connect the output of the Assignment block directly back to the input of the vector for the next iteration. This approach optimizes memory usage and maintains the integrity of your vector throughout the loop.

Hope this helps.

John
John 5 minutes ago
Hi @Umar,
Can you please share a screenshot of the Simulink implementation that you are suggesting or attach the model itself?
Thanks,

Sign in to comment.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Products


Release

R2017a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!