generate column array with day in year number for each hour in a year

조회 수: 3 (최근 30일)
Mayk
Mayk 2021년 10월 22일
답변: Mayk 2021년 10월 22일
Dear,
i want a column array with length equal to hours in a year: 365d*24h = 8760
the content of the array should be the number of the day, corresponding with the hours, so:
1,1,1,1,1,..1,2,2...,2,3,3,...,3... - 365,365,...,365. (total length: 8760 values, number 1 should be repeated 24 times, then number 2 repeated 24 times, etc).
How to code this efficiently?
What i tried was a for-loop, but i want to use a more efficient way:
days = zeros(24,1);
all_days = days;
for i = 1:365-1
all_days = [all_days; days+i];
end

채택된 답변

KSSV
KSSV 2021년 10월 22일
r=repmat(0:364,24,1) ;
r=r(:);
  댓글 수: 1
Mayk
Mayk 2021년 10월 22일
편집: Mayk 2021년 10월 22일
wow genius! i did know repmat, but i didn't know that using r=r(:); puts everything in one column!
r = r(:); % this converts matrix into a one column array

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

추가 답변 (1개)

Mayk
Mayk 2021년 10월 22일
using this improvement, my time to run this section of code decrease from:
Elapsed time is 0.002214 seconds.
to
Elapsed time is 0.000130 seconds.
thanks!
measured by puting 'tic' before code and 'toc' at end of code to measure time to run code.

카테고리

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

제품


릴리스

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by