Accumulate values in time series
조회 수: 1 (최근 30일)
이전 댓글 표시
I have a time series matrix like this:
...
cod_st yy mm dd hh mm prec_mm
02549075 1981 03 25 07 00 0.7
02549075 1981 03 25 17 00 2.0
02549075 1981 03 26 07 00 3.5
02549075 1981 03 26 17 00 1.0
02549075 1981 03 27 07 00 1.0
02549075 1981 03 27 17 00 6.3
02549075 1981 03 28 07 00 19.6
02549075 1981 03 28 17 00 7.2
02549075 1981 03 29 07 00 1.2
02549075 1981 03 29 17 00 0.0
02549075 1981 03 30 07 00 0.0
...
I need to sum the value (:,7) measured at 5pm with the next value (7am of subsequent day), getting this:
...
02549075 1981 03 25 07 00 0.7
02549075 1981 03 26 07 00 5.5
02549075 1981 03 27 07 00 2.0
02549075 1981 03 28 07 00 25.9
02549075 1981 03 29 07 00 8.4
02549075 1981 03 30 07 00 0.0
...
How can I do that in MatLab?
답변 (1개)
per isakson
2016년 12월 7일
편집: per isakson
2016년 12월 7일
Complaint: Matlab doesn't offer an easy and intuitive way to "Accumulate values in time series". Neither time series nor timetable seems to help much.
I guess that the input data you show is in a text file. Anyhow, I copied it to cssm.txt.
I assume that no other values of hour than 07 and 17 exist in the file.
Try
>> [ acc, sdn ] = cssm('cssm.txt');
>> acc'
ans =
0.7000 5.5000 2.0000 25.9000 8.4000 0
>> datestr( sdn(1:3), 'yyyy-mm-dd HH:MM:SS' )
ans =
1981-03-25 07:00:00
1981-03-26 07:00:00
1981-03-26 07:00:00
where
function [ val, sdn ] = cssm( filespec )
fid = fopen( filespec, 'r' );
cac = textscan( fid, '%f%d%d%d%d%d%f' ...
, 'Headerlines',1, 'CollectOutput',true );
[~] = fclose( fid );
%
dt_vec = cat( 2, cac{2}, zeros( size(cac{2},1), 1 ) );
prec_mm = cac{3};
subs = datenum( double( dt_vec(:,1:3) ) ) ...
- datenum( double( dt_vec(1,1:3) ) ) + 1;
%
subs( dt_vec(:,4)==17 ) = subs( dt_vec(:,4)==17 ) + 1;
%
val = accumarray( subs, prec_mm, [], @sum, nan );
%
sdn = datenum( double( dt_vec(1,1:3) ) ) + subs - 1 + 7/24;
end
Tested on R2016a
댓글 수: 0
참고 항목
카테고리
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!