Synchronize two time series matlab R2015a
조회 수: 2 (최근 30일)
이전 댓글 표시
I have two time series SP and CAC40 returns, i would like to save only equal dates values, i have a matlab 2015a. Bests
채택된 답변
Star Strider
2018년 3월 2일
Try this:
[D1,S1,R1] = xlsread('SP dated.xls');
[D2,S2,R2] = xlsread('CAC40 dated.xls');
S1ds = R1(2:end,:); % Valid ‘SP’ Data (Omitting Header)
S2ds = R2(2:end,:); % Valid ‘CAC40’ Data (Omitting Header)
dn1 = datenum(S1ds(:,1), 'yyyy-mm-dd'); % Convert To ‘Date Numbers’
dn2 = datenum(S2ds(:,1), 'yyyy-mm-dd'); % Convert To ‘Date Numbers’
[Dc, ia, ic] = intersect(dn1, dn2, 'stable'); % Common Date Numbers
SP = S1ds(ia,:); % ‘SP’ Result Matrix
CAC40 = S2ds(ic,:); % ‘CAC40’ Result Matrix
SP_First10 = SP(1:10,:); % Examine Output (Delete Later)
CAC40_First10 = CAC40(1:10,:); % Examine Output (Delete Later)
The code is straightforward, and the comments document what each line does. The 'stable' argument to the intersect call keeps everything in the original order rather than sorting it.
The sizes of the output arrays ‘SP’ and ‘CAC40’ are the same sizes (as they should be), and both less than the original of either of the original arrays, indicating that they now have only common dates. I looked at a range of them but not all, and the result appears to be what you want.
I included ‘SP_First10’ and ‘CAC40_First10’ to let you easily examine that range or any other range you want, to verify that the data are correct.
추가 답변 (1개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Calendar에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!