Finding the difference in timeseries values
조회 수: 34 (최근 30일)
이전 댓글 표시
If I have the below timetable, how do I find the difference between values? Diff() doesn't work. I've tried diff(TableName.Variables), however that removes the Time column and VariableNames.
Time Humidity TemperatureF PressureHg
____________________ ________ ____________ __________
15-Nov-2015 00:00:00 48.9 51.45 29.61
15-Nov-2015 06:00:00 48.9 51.45 29.6
15-Nov-2015 12:00:00 49.025 51.45 29.61
15-Nov-2015 18:00:00 48.9 51.225 29.607
16-Nov-2015 00:00:00 48.5 51.4 29.61
댓글 수: 0
답변 (3개)
Star Strider
2021년 4월 4일
This is kludgy, however I can find no other way to do what you want:
C = {'15-Nov-2015 00:00:00' 48.9 51.45 29.61
'15-Nov-2015 06:00:00' 48.9 51.45 29.6
'15-Nov-2015 12:00:00' 49.025 51.45 29.61
'15-Nov-2015 18:00:00' 48.9 51.225 29.607
'16-Nov-2015 00:00:00' 48.5 51.4 29.61};
T1 = cell2table(C, 'VariableNames',{'Time','Humidity','TemperatureF','PressureHg'}) % Create Table From Data
T1.Time = datetime(T1{:,1}, 'InputFormat','dd-MMM-yyyy HH:mm:ss'); % Create Table From Data
DiffArray = diff(T1{:,2:4}) % Column Differences
DT = table(datetime(diff(datenum(T1{:,1})),'ConvertFrom','datenum','Format','HH:mm'),'VariableNames',{'DiffTime'}); % Time Differences
T2 = [DT array2table(DiffArray, 'VariableNames',{'DiffHumidity','DiffTemperatureF','DiffPressureHg'})]
producing:
T2 =
4×4 table
DiffTime DiffHumidity DiffTemperatureF DiffPressureHg
________ ____________ ________________ ______________
06:00 0 0 -0.01
06:00 0.125 0 0.01
06:00 -0.125 -0.225 -0.003
06:00 -0.4 0.175 0.003
.
댓글 수: 0
Peter Perkins
2021년 12월 7일
David, you are correct that diff(TableName.Variables) removes the times and the variable names, in fact, it returns a numeric matrix not a timetable!
Applying diff gives you one fewer rows, so one way or another you need to create a new timetable. If you are only diff'ing one variable, then
ttDiff = timetable(diff(tt.X),'RowTimes',tt.Time(2:end),'VariableNames',{'X_diff'})
is one way to go. For three variables (maybe even for just one in this case), varfun is probably the best:
>> tt
tt =
5×3 timetable
Time Humidity TemperatureF PressureHg
____________________ ________ ____________ __________
15-Nov-2015 00:00:00 48.9 51.45 29.61
15-Nov-2015 06:00:00 48.9 51.45 29.6
15-Nov-2015 12:00:00 49.025 51.45 29.61
15-Nov-2015 18:00:00 48.9 51.225 29.607
16-Nov-2015 00:00:00 48.5 51.4 29.61
>> ttDiff = varfun(@diff,tt)
ttDiff =
4×3 timetable
Time diff_Humidity diff_TemperatureF diff_PressureHg
____________________ _____________ _________________ _______________
15-Nov-2015 00:00:00 0 0 -0.01
15-Nov-2015 06:00:00 0.125 0 0.01
15-Nov-2015 12:00:00 -0.125 -0.225 -0.003
15-Nov-2015 18:00:00 -0.4 0.175 0.003
There's also a long example in the doc that talks about "how to perform calculations by using the numeric and categorical data that the table contains" at length:
댓글 수: 0
Siddharth Bhutiya
2023년 3월 30일
If you are using R2023a, then you can now directly call diff on a timetable. Here is an example,
>> tt
tt =
5x3 timetable
Time Humidity TemperatureF PressureHg
____________________ ________ ____________ __________
15-Nov-2015 00:00:00 48.9 51.45 29.61
15-Nov-2015 06:00:00 48.9 51.45 29.6
15-Nov-2015 12:00:00 49.025 51.45 29.61
15-Nov-2015 18:00:00 48.9 51.225 29.607
16-Nov-2015 00:00:00 48.5 51.4 29.61
>> diff(tt)
ans =
4x3 timetable
Time Humidity TemperatureF PressureHg
____________________ ________ ____________ __________
15-Nov-2015 00:00:00 0 0 -0.01
15-Nov-2015 06:00:00 0.125 0 0.01
15-Nov-2015 12:00:00 -0.125 -0.225 -0.003
15-Nov-2015 18:00:00 -0.4 0.175 0.003
See this documentation page for other arithmetic operations that are now allowed on tables and timetables.
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Tables에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!