MATLAB Answers

0

Fill time series with data coming from other time series.

Asked by Tommaso Belluzzo on 11 Mar 2017
Latest activity Answered by Guillaume
on 11 Mar 2017
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?

  0 Comments

Sign in to comment.

2 Answers

Answer by Star Strider
on 11 Mar 2017
 Accepted Answer

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’.

  0 Comments

Sign in to comment.


Answer by Guillaume
on 11 Mar 2017

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'

  0 Comments

Sign in to comment.