How to combine matrixes of different lengths in order of date?

1 Ansicht (letzte 30 Tage)
MC3105
MC3105 am 25 Okt. 2014
Kommentiert: MC3105 am 25 Okt. 2014
Hey everyone,
I am wondering whether there exists a simple way in matlab to do the following:
I got three matrixes. Each matrix consists of two colums of which the first column contain dates. These date vectors have different lengths in each matrix and sometimes there are gaps between some time steps. For example in one matrix the date 12-27-2010 might be missing, while the date exists in the other two matrixes. The second column in each matrix contains my data. Now I want to combine the three matrixes into one matrix. The first column of this new matrix is supposed to contain a date vector without any gaps between time steps. As the date vectors in my three matrixes are not exactly the same (because of gabs and differen lengths) I cannot simply do copy-paste with my data. I need to order them by date somehow. Please see my example for a better explanation of what I want.
Example:
Matrix 1
A=[2010-12-27 1
2010-12-28 3
2010-12-29 2
2010-12-30 4]
Matrix 2:
B=[2010-12-26 1
2010-12-28 5
2010-12-29 3
2010-12-30 4]
Matrix 3:
C=[2010-12-27 1
2010-12-28 1
2010-12-30 5]
I want to get the following matrix:
D=[2010-12-26 0 1 0
2010-12-27 1 0 1
2010-12-28 3 5 1
2010-12-29 2 3 0
2010-12-30 4 4 5]
Later I want to add all three data colums to get:
E=[2010-12-26 1
2010-12-27 2
2010-12-28 9
2010-12-29 5
2010-12-30 13]
So if there is a simple way to just to this last step and skip the creation of matrix D I'd be grateful for your ideas, too!!
Thanks so much!!

Akzeptierte Antwort

Guillaume
Guillaume am 25 Okt. 2014
accumarry can perform the last step. The 3rd return value of unique applied to the concatenation of the dates (as datenum) will give you the subs for accumarray:
A=[datenum('2010-12-27') 1
datenum('2010-12-28') 3
datenum('2010-12-29') 2
datenum('2010-12-30') 4];
B=[datenum('2010-12-26') 1
datenum('2010-12-28') 5
datenum('2010-12-29') 3
datenum('2010-12-30') 4];
C=[datenum('2010-12-27') 1
datenum('2010-12-28') 1
datenum('2010-12-30') 5];
[dates, ~, subs] = unique([A(:, 1); B(:,1); C(:,1)]);
E = [dates, accumarray(subs, [A(:, 2); B(:, 2); C(:, 2)])]
  8 Kommentare
Guillaume
Guillaume am 25 Okt. 2014
Can you attach an example file?
MC3105
MC3105 am 25 Okt. 2014
it's ok i figured out a way now using a loop :) probably less efficient than your way but whatever :) thanks again!!

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Time Series Objects finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by