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

1 visualizzazione (ultimi 30 giorni)
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!!

Risposta accettata

Guillaume
Guillaume il 25 Ott 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 Commenti
MC3105
MC3105 il 25 Ott 2014
it's ok i figured out a way now using a loop :) probably less efficient than your way but whatever :) thanks again!!

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Dates and Time in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by