Clear Filters
Clear Filters

How to synchronize matrices (timeseries)?

1 view (last 30 days)
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!

Accepted Answer

Léon
Léon on 15 Sep 2011
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 Comment
Fangjun Jiang
Fangjun Jiang on 15 Sep 2011
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

Sign in to comment.

More Answers (3)

Fangjun Jiang
Fangjun Jiang on 15 Sep 2011
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 on 16 Sep 2011
Thank you that was exactly the point!

Léon
Léon on 26 Sep 2011
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 Comments
Fangjun Jiang
Fangjun Jiang on 26 Sep 2011
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 on 26 Sep 2011
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.

Sign in to comment.

Categories

Find more on Financial Toolbox in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!