<randum number generation

조회 수: 3 (최근 30일)
Johannes Deelstra
Johannes Deelstra 2014년 3월 18일
댓글: Johannes Deelstra 2014년 3월 28일
Hei, I have a time series for day and observation for a period of 365 days in variable a, a(:,1) being the daynumber (1 - 365) and a(:,2) the observations. I need to randomly select a day every week, so the first selection has to be a number from 1 - 7, the second day has to be a number between 8 - 14, etc. The selected daynumber have to be entered in a new variable (b). How is this done? Thanks for helping, Johannes.

답변 (2개)

Carlos
Carlos 2014년 3월 18일
편집: Carlos 2014년 3월 18일
In r you have the numbers you requested
r=zeros(52,1);
pointer=0;
for k=1:52
r(k)=pointer+(1+round(7*rand));
pointer=pointer+7;
end

Niklas Nylén
Niklas Nylén 2014년 3월 18일
If we assume that the first day in the array also is the start of a week it is necessary to generate 52 random days during a 365 day period. Since 52*7 = 364 it the final day must be day 365 since it is the only day in that week.
Start of by generating 52 random integers in the range [1,7].
randomWeekdays = randi(7,1,52);
Next, translate the numbers to days in the year
b = randomWeekdays+(0:7:358);
  댓글 수: 1
Johannes Deelstra
Johannes Deelstra 2014년 3월 28일
A bit late but thanks very much to both, answering my question concerning random number generation. Regards Johannes

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

카테고리

Help CenterFile Exchange에서 Calendar에 대해 자세히 알아보기

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by