Fill time series with data coming from other time series.

4 Ansichten (letzte 30 Tage)
Tommaso Belluzzo
Tommaso Belluzzo am 11 Mär. 2017
Beantwortet: Guillaume am 11 Mär. 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?

Akzeptierte Antwort

Star Strider
Star Strider am 11 Mär. 2017
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’.

Weitere Antworten (1)

Guillaume
Guillaume am 11 Mär. 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'

Kategorien

Mehr zu Descriptive Statistics finden Sie in Help Center und File Exchange

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by