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

조회 수: 1 (최근 30일)
MC3105
MC3105 2014년 10월 25일
댓글: MC3105 2014년 10월 25일
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!!

채택된 답변

Guillaume
Guillaume 2014년 10월 25일
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
Guillaume
Guillaume 2014년 10월 25일
Can you attach an example file?
MC3105
MC3105 2014년 10월 25일
it's ok i figured out a way now using a loop :) probably less efficient than your way but whatever :) thanks again!!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Dates and Time에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by