Why slow conversion from date in Matlab time to day of year?
조회 수: 4 (최근 30일)
이전 댓글 표시
I have a vector of dates in Matlab time convention which I need to convert to day of the year. The following code is slow (9 seconds) even though it is using builtin Matlab functions. What do you recommend to speed it up?
timeGmt = datenum(1996,1,1):1/24:now; % Long vector of dates covering many years
timeGmt = timeGmt'; % Change from row vector to column vector for compatibility in next line
yearDay = timeGmt - ...
datenum(str2num(datestr(timeGmt, 10)), 1, 1) ... % Subtract off first day of this year
+ 1; % Add a day so that first day of year is day 1
댓글 수: 0
채택된 답변
James Tursa
2015년 5월 18일
편집: James Tursa
2015년 5월 18일
Try this variation using datevec instead of datestr:
vecGmt = datevec(floor(timeGmt)); % Set hms to 0's using floor
vecGmt(:,2:3) = 1; % Set month and day to Jan 1
yearDay = timeGmt - datenum(vecGmt) + 1;
댓글 수: 3
James Tursa
2015년 5월 18일
Try the edited version using floor instead of setting the hms to 0's directly.
추가 답변 (1개)
Peter Perkins
2015년 5월 18일
편집: Walter Roberson
2015년 5월 19일
In R2014b or later, use datetime. On a not particularly fast Win7 machine:
>> timeGmt = '1-Jan-1996':hours(1):datetime('now');
>> tic, dayOfYear = day(timeGmt,'dayofyear'); toc
Elapsed time is 0.041145 seconds.
댓글 수: 2
Peter Perkins
2015년 6월 3일
If you have R2014b or later, you have the day function. It is a method of the datetime class, which was introduced in R2014b.
참고 항목
카테고리
Help Center 및 File Exchange에서 Dates and Time에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!