Fill time series with data coming from other time series.

3 次查看(过去 30 天)
I have the following matrix:
737044 NaN NaN NaN
737045 NaN NaN NaN
737046 NaN NaN NaN
737047 NaN NaN NaN
737048 NaN NaN NaN
737049 NaN NaN NaN
737050 NaN NaN NaN
737051 NaN NaN NaN
737052 NaN NaN NaN
737053 NaN NaN NaN
In which the first column represents the date (datenum) and the other columns need to be filled with data coming from other time series. Let's say I want to fill the second column of this matrix with the following data, merging the values according to the date:
737045 12.33
737047 15.01
737051 14.77
737052 14.63
737053 13.89
I'm aiming to obtain the following result:
737044 NaN NaN NaN
737045 12.33 NaN NaN
737046 NaN NaN NaN
737047 15.01 NaN NaN
737048 NaN NaN NaN
737049 NaN NaN NaN
737050 NaN NaN NaN
737051 14.77 NaN NaN
737052 14.63 NaN NaN
737053 13.89 NaN NaN
What is the best way to achieve this? How to deal with missing (NaN) values if, for example, I want to perform a regression? Interpolation? Leaving them as they are?

采纳的回答

Star Strider
Star Strider 2017-3-11
This works:
M1 = [737044 NaN NaN NaN
737045 NaN NaN NaN
737046 NaN NaN NaN
737047 NaN NaN NaN
737048 NaN NaN NaN
737049 NaN NaN NaN
737050 NaN NaN NaN
737051 NaN NaN NaN
737052 NaN NaN NaN
737053 NaN NaN NaN];
M2 = [737045 12.33
737047 15.01
737051 14.77
737052 14.63
737053 13.89];
CommonElements = ismember(M1(:,1), M2(:,1));
Result = M1;
Result(CommonElements,2) = M2(:,2)
Result =
7.3704e+05 NaN NaN NaN
7.3705e+05 12.33 NaN NaN
7.3705e+05 NaN NaN NaN
7.3705e+05 15.01 NaN NaN
7.3705e+05 NaN NaN NaN
7.3705e+05 NaN NaN NaN
7.3705e+05 NaN NaN NaN
7.3705e+05 14.77 NaN NaN
7.3705e+05 14.63 NaN NaN
7.3705e+05 13.89 NaN NaN
It uses the logical vector returned by ismember to address the appropriate elements of ‘M1’ and replace them with the second column of ‘M2’.

更多回答(1 个)

Guillaume
Guillaume 2017-3-11
The simplest way to do what you want may be to use time tables. It is to synchronise time tables, and you've got a choice of methods for the missing values (fill with missing, fill with nearest, interpolate, etc.).
Simply convert your outdated datenum in datetime objects, and convert to timetable.
tt = array2timetable(yourmatrix(:, 2:end), 'RowTimes', datetime(yourmatrix(:, 1), 'ConvertFrom', 'datenum')); %optionally give names to columns with 'VariableNames'

类别

Help CenterFile Exchange 中查找有关 Calendar 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by