Adding to an array with constraints

4 vues (au cours des 30 derniers jours)
sasso samer
sasso samer le 25 Juil 2016
Commenté : sasso samer le 25 Juil 2016
I have a vector A = [0.2 0.3 0.1 0.3 0.4 0.0]
and i will add a random number to the minimum number of vector A which is here 0. But i have a constraint that the sum of every 2 consecutive numbers can't exceed 0.5 and if the random number which is added made it exceed the 0.5 then add another random number untill the condition is satidfied
  2 commentaires
Image Analyst
Image Analyst le 25 Juil 2016
A robust solution would require you to specify the range of the input values in A, and the range of the random numbers. Otherwise, just add random numbers between minus 2 billion and minus 1 billion to your array and that will most likely work, at least for the A you have here.
sasso samer
sasso samer le 25 Juil 2016
rand(0,10)/100

Connectez-vous pour commenter.

Réponses (1)

Azzi Abdelmalek
Azzi Abdelmalek le 25 Juil 2016
A=[0.2 0 0.3 0.1 0.3 0.4 0.0]
B=[eps a eps]
idx=find(B==0)
for k=idx
c=max(B(idx-1),B(idx+1))
A(idx-1)=(0.5-c)*rand
end
  1 commentaire
sasso samer
sasso samer le 25 Juil 2016
what is vector B?

Connectez-vous pour commenter.

Catégories

En savoir plus sur Random Number Generation dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by