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

55 vues (au cours des 30 derniers jours)
John
John le 19 Nov 2024 à 21:50
Commenté : Umar le 22 Nov 2024 à 2:30
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

Réponses (1)

Umar
Umar le 20 Nov 2024 à 9:19

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.

  4 commentaires
John
John le 21 Nov 2024 à 7:57
Hi @Umar,
Can you please share a screenshot of the Simulink implementation that you are suggesting or attach the model itself?
Thanks,
Umar
Umar le 22 Nov 2024 à 2:30

Hi @John,

I do apologize since I have access to Matlab mobile but could not afford Simulink. I can provide the links to blocks listed below which will help you out to understand by implementing the model.

Inport block

https://www.mathworks.com/help/simulink/slref/inport.html

Assignment Block

Assignment block

Switch block

Switch block link

Connectez-vous pour commenter.

Catégories

En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange

Produits


Version

R2017a

Community Treasure Hunt

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

Start Hunting!

Translated by