필터 지우기
필터 지우기

How do i create an array with a for loop?

조회 수: 3 (최근 30일)
Brendan Querey
Brendan Querey 2020년 11월 17일
댓글: Walter Roberson 2020년 11월 17일
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

채택된 답변

Walter Roberson
Walter Roberson 2020년 11월 17일
편집: Walter Roberson 2020년 11월 17일
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
Brendan Querey
Brendan Querey 2020년 11월 17일
what does @() do?
Walter Roberson
Walter Roberson 2020년 11월 17일
@() 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.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Random Number Generation에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by