Effacer les filtres
Effacer les filtres

Info

Cette question est clôturée. Rouvrir pour modifier ou répondre.

I want to change a variable with every iteration either randomly, how do i do that ?

1 vue (au cours des 30 derniers jours)
Panth Patel
Panth Patel le 22 Oct 2020
Clôturé : MATLAB Answer Bot le 20 Août 2021
I want to change a variable with every iteration either by +1 or -1 randomly and keep the overall variable in between +5 to -5

Réponses (1)

Ameer Hamza
Ameer Hamza le 22 Oct 2020
Something like this will work
x = 0;
for i = 1:100
x = min(max(x + 2*(rand()>0.5)-1, -5), 5)
end
  2 commentaires
Panth Patel
Panth Patel le 24 Oct 2020
Modifié(e) : Panth Patel le 24 Oct 2020
It works fine
Thank you very much !!!
Your help is very much appreciated.

Cette question est clôturée.

Community Treasure Hunt

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

Start Hunting!

Translated by