How to synchronize matrices (timeseries)?

1 次查看(过去 30 天)
May Somebody help me solve this problem?
I retrieved stock data from Yahoo and stored it in matrices, every matrix consists of 6 Columns (date,open,high,low,close,volume) and N time steps in the rows (daily).
conn = yahoo('http://download.finance.yahoo.com');
DBK=fetch(conn,'DBK.DE',{'Open', 'High', 'Low','Close','Volume'},'Jan 01 2005','Dec 31 2010', 'd')
DTE=fetch(conn,'DTE.DE',{'Open', 'High', 'Low','Close','Volume'},'Jan 01 2005','Dec 31 2010', 'd')
You will notice that both matrices do not have the same amount of rows DBK<1547x6>; DTE<1528x6>
Hence I cannot compare them, since there are some missing values in DTE. But since I got the date ID in column 1 of each matrix I would like to have the intersection between both matrices based on this ID. Can you help me? And is it possible to compute the intersection for more than 2 matrices?
Help is greatly appreciated!

采纳的回答

Léon
Léon 2011-9-15
That is exactly what I need, but I have to find the intersection between more than 2 matrices. :-( I tried to use the intersect() command iterative (intersection between A/B, B/C, C/D,…) but this doesn't really work.
Do you have another hint for me, please?
  1 个评论
Fangjun Jiang
Fangjun Jiang 2011-9-15
You can do one at a time, like
out=intersect(A,B);
out=intersect(out,C);
out=intersect(out,D);
or see http://www.mathworks.com/matlabcentral/fileexchange/30725-multiple-or-partial-intersect-function

请先登录,再进行评论。

更多回答(3 个)

Fangjun Jiang
Fangjun Jiang 2011-9-15
Did you try intersect()?
A=[(1:10)', rand(10,1)];
B=[(5:15)', rand(11,1)];
[C,IndA,IndB]=intersect(A(:,1),B(:,1));
A_Selected=A(IndA,:)
B_Selected=B(IndB,:)

Léon
Léon 2011-9-16
Thank you that was exactly the point!

Léon
Léon 2011-9-26
I have a follow-up question and I hope you can help me again:
I tried to solve this problem using loops and stored the data in cell arrays. Applying your code to that did not quite work, since I managed to get the intersection of all time-series, but now I want to reduce all matrices based upon this intersection:
% N = number of time-series
for n = 1:N,
% Get the intersection of all time-series (date is stored in column 1)
AnB = intersect(data{n,1}(:,1),data{n,1}(:,1));
end;
for n = 1:N,
% Now I want to "delete" all rows that are not part of the intersection
% But of course I want to have all columns with the data in it and not only the date column
[c, a, b] = intersect(AnB(:,1),data{n,1}(:,1));
data{n,1} = data{n,1}(a,:); % --> this gives the error
end;
Thank you very much for helping me!
  2 个评论
Fangjun Jiang
Fangjun Jiang 2011-9-26
Please post this as a separate question.
And you need to take a second look at the question. None of the variable N, data are given so others won't be able to run the code.
What is AnB = intersect(data{n,1}(:,1),data{n,1}(:,1))?
Do you mean AnB = intersect(data{n,1}(:,1),data{n,2}(:,1))?
Léon
Léon 2011-9-26
OK, sorry for that. I opened a new question: http://www.mathworks.de/matlabcentral/answers/16786-intersection-of-multiple-time-series and reworked the code.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Financial Toolbox 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by