Effacer les filtres
Effacer les filtres

How do i create an array with a for loop?

3 vues (au cours des 30 derniers jours)
Brendan Querey
Brendan Querey le 17 Nov 2020
Commenté : Walter Roberson le 17 Nov 2020
I'm trying to create a an array of ten numbers randomly generate from a normal distribution, and I also need the initial number to not have the same function as the remaining 9.
P_t = normrnd(72.7,7.6);
p_t = normrnd(12.6,3.9);
price = zeros(10:1);
for n = 1:10
if n == 1
price(n) = P_t;
else
price(n) = .8 * P_t + p_t;
end
end

Réponse acceptée

Walter Roberson
Walter Roberson le 17 Nov 2020
Modifié(e) : Walter Roberson le 17 Nov 2020
P_t = @() normrnd(72.7,7.6);
p_t = @() normrnd(12.6,3.9);
price = zeros(10,1);
for n = 1:10
if n == 1
price(n) = P_t();
else
price(n) = .8 * P_t() + p_t();
end
end
  2 commentaires
Brendan Querey
Brendan Querey le 17 Nov 2020
what does @() do?
Walter Roberson
Walter Roberson le 17 Nov 2020
@() introduces the definition of an anonymous function that does not expect any parameters.
In the above code, each time P_t() is encountered, it would execute the function body, normrnd(72.7,7.6) , generating a new random number with the given distribution.
The code you were using was generating one single random number from each of the two distributions, and then using that same number for each iteration.

Connectez-vous pour commenter.

Plus de réponses (0)

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