Reshape a column vector containing several stocks returns into a matrix format
Show older comments
Hello everyone,
I have a column vector containing dates and the respective stock prices for a large set of stocks with different starting and ending dates (as some stocks were only listed after the start of the sample or were delisted during the sample period). Hence, the first 100 rows of the vector might containg the dates and stock returns of stock 1, rows 101 to 350 the dates and prices of stock 2, rows 351 to 400 the same data of stock 3 and so on. I obtained the data set from the Wharton CRSP database.
Now, i would like to transform this column vector into a matrix which contains the dates for the largest available time period in the a first column and the respective stock prices in the subsequent columns such that column 2 contains the stock prices of stock 1, column 3 the stock prices of stock 2 and so on.
I already tried several merging or reshaping commands without any success. Therefore, I would be very thankful for any advice.
Kind regards, Maximilian
5 Comments
Cedric
on 24 Jun 2014
Could you provide a sample or build a small numeric example?
Image Analyst
on 24 Jun 2014
How do you know which row of the column vector begins a run with a new stock? What form is the date in? How do you have both dates and prices in a column vector? Are they interleaved? Or is it a 2 column array or cell array?
Maximilian
on 25 Jun 2014
Hi Maximilian, I am traveling and I will come back on your problem tomorrow. It is not too complicated actually.
There is no such thing as an empty element/content in numeric arrays (there would be one in cell arrays, but the latter are not optimal for storing large arrays of numbers). How will you use this new array of PERMCOs restructured in column afterwards? Would it be ok/usable if we filled it with NaNs where there is no value?
Maximilian
on 26 Jun 2014
Accepted Answer
More Answers (1)
Maximilian
on 27 Jun 2014
Edited: Maximilian
on 27 Jun 2014
1 vote
1 Comment
Great, you've been lucky, I got stuck at the airport and I spent 25 minutes on this to change my mind! Note that there is a more concise but more complex approach which consists in building both permco and date IDs, and build the prices array in one shot instead of the loop. I thought that the loop would be simpler to grasp though.
Cheers,
Cedric
Categories
Find more on Financial Data Analytics 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!