Interpolate timeseries of a 4D matrix (4th D as time)

53 次查看(过去 30 天)
Hey experts, please could you help me to figure this out?
I am trying to interpolate a 4D matrix, in which the 4th dimension is time. It has 12 records (monthly), but I want it each 3 days, so it'll be 121 records).
For this I am trying both ways below:
load data.mat
dtStr = ["15/01/2016","15/02/2016","15/03/2016","15/04/2016","15/05/2016","15/06/2016",...
"15/07/2016","15/08/2016","15/09/2016","15/10/2016","15/11/2016","15/12/2016"];
dtvA=datevec(dtStr,'dd/mm/yyyy');
dttA = datetime(dtvA);
timeS = datestr((time/86400)+datenum(1958,1,1));
dttB = datetime(timeS);
tmtb = timetable(dttA,Nit2);
NO3_clim_3daily = retime(tmtb,'regular','linear','TimeStep',days(3));
% Or
NO3_clim_3daily = retime(tmtb,dttB,'linear');
But it give me the error: "Error using timetable/retime (line 140). Interpolation requires at least two sample points in each dimension".
Why am I getting this error, if I have a matrix of 31x11x37x12?
I still could not figure it out and how to solve it. Please, could someone help me?
Thanks!

采纳的回答

Stephen23
Stephen23 2024-11-6,17:10
编辑:Stephen23 2024-11-6,17:21
Avoid using deprecated DATENUM & DATESTR, using DATETIME is much better.
S = load('data.mat')
S = struct with fields:
Nit2: [31x11x37x12 double] time: [121x1 double]
dttA = datetime(2016,1:12,15);
dttB = datetime(S.time(:),'convertfrom','epochtime','Epoch',datetime(1958,1,1));
"Why am I getting this error, if I have a matrix of 31x11x37x12?"
Note that for a TIMETABLE the first dimension (i.e. rows) must correspond to the time: in contrast the 4th dimension of your array has size 12, thus seems to correspond to the 12 months you specified. So we would need to permute that array so that it fits the TIMETABLE definition:
tmtb = timetable(dttA(:),permute(S.Nit2,[4,1,2,3]))
tmtb = 12x1 timetable
Time Var1 ___________ _________________ 15-Jan-2016 1x31x11x37 double 15-Feb-2016 1x31x11x37 double 15-Mar-2016 1x31x11x37 double 15-Apr-2016 1x31x11x37 double 15-May-2016 1x31x11x37 double 15-Jun-2016 1x31x11x37 double 15-Jul-2016 1x31x11x37 double 15-Aug-2016 1x31x11x37 double 15-Sep-2016 1x31x11x37 double 15-Oct-2016 1x31x11x37 double 15-Nov-2016 1x31x11x37 double 15-Dec-2016 1x31x11x37 double
after that RETIME is exactly as you showed:
NO3_clim_3daily = retime(tmtb,'regular','linear','TimeStep',days(3))
NO3_clim_3daily = 113x1 timetable
Time Var1 ___________ _________________ 15-Jan-2016 1x31x11x37 double 18-Jan-2016 1x31x11x37 double 21-Jan-2016 1x31x11x37 double 24-Jan-2016 1x31x11x37 double 27-Jan-2016 1x31x11x37 double 30-Jan-2016 1x31x11x37 double 02-Feb-2016 1x31x11x37 double 05-Feb-2016 1x31x11x37 double 08-Feb-2016 1x31x11x37 double 11-Feb-2016 1x31x11x37 double 14-Feb-2016 1x31x11x37 double 17-Feb-2016 1x31x11x37 double 20-Feb-2016 1x31x11x37 double 23-Feb-2016 1x31x11x37 double 26-Feb-2016 1x31x11x37 double 29-Feb-2016 1x31x11x37 double
NO3_clim_3daily = retime(tmtb,dttB)
NO3_clim_3daily = 121x1 timetable
Time Var1 ___________ _________________ 02-Jan-2016 1x31x11x37 double 05-Jan-2016 1x31x11x37 double 08-Jan-2016 1x31x11x37 double 11-Jan-2016 1x31x11x37 double 14-Jan-2016 1x31x11x37 double 17-Jan-2016 1x31x11x37 double 20-Jan-2016 1x31x11x37 double 23-Jan-2016 1x31x11x37 double 26-Jan-2016 1x31x11x37 double 29-Jan-2016 1x31x11x37 double 01-Feb-2016 1x31x11x37 double 04-Feb-2016 1x31x11x37 double 07-Feb-2016 1x31x11x37 double 10-Feb-2016 1x31x11x37 double 13-Feb-2016 1x31x11x37 double 16-Feb-2016 1x31x11x37 double
  1 个评论
Luís Henrique Bordin
Luís Henrique Bordin 2024-11-6,18:41
Thank you so much, Stephan,
I know, if I do not permute, the timetable don't work, I did that before writing the code here to ask for help, and should have forgot to include it in the example.
Well, the data I gave you is a subset of the real matrix, since the file was too big to attach.
When trying to apply to my real matrix, it didn't work, and just then I realized the issue it wasn't working, it was because the real matrix had NaNs, and the subset I gave to you does not. By replacing the NaNs by zero, it worked.
Anyway, I learned from your code, so thank you very much for that!
Best,
Luis

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Time Series Events 的更多信息

产品


版本

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by