Switching individual columns in a large matrix

5 views (last 30 days)
I have been working on some time series data in the past few week and this project is about to approach the deadline. To describe, basically I have a bunch of equity data that are not matched according to their dates (i.e.: some got delisted etc.). So in order to make financial time series to work for this, I really need to sort it. Currently, I have a matrix where it is padded with trailing 0's because I had to switch the original data of only containing an entire row of listings to different columns for each equity.
Having said that, the first row of my matrix for example for all the columns now have all the dates mixed up. For example suppose I have a 6x6 matrix in the attached photos of the upper matrix (my bad about the spacing). But what I want is the 2nd matrix where the dates are sorted in order. This can be easily done but most importantly is how I can also shift the corresponding values (i.e.: such as how the element in (row1,column5 and column 6) switched with a row of padded 0's given that the data was missing the date 02/01/93 for equity described in row 5 and 6.
The date can be easily created with the command of creating dates but I'm not sure how to switch the rows corresponding to only those values. This is very important to me because I need to be able to match the date with the corresponding values so that each row has a certain date.
Can anyone help me with this ?
Thanks
Basically I have the first column as a column of datenum as well as col 4, 7, 10, 13 etc where each of these represents a date of observation for a specific sample observed. Column 3,6,9,12 are the corresponding variable of interest respectively.
As you can see from column 13, it follows that some are padded with 0's because there aren't enough observations to be present through the entire sampling period and so they are padded with 0's. However, column 13's and column 12 should not be placed at row 1 (they should be place at the row where it is equal to column 1 (and this is given by a row of 693, which is 724519 and this would be the same date as 724520 which I will fix [some issue with the time specification])... So, I basically need to move row 1 , column 12 and 13 to row 693 instead while replacing the original element with 0. So in a sense I'm shifting everything up. I have a code but its not fully working so far but i think it is heading in the right direction...**
for n = 1:1069 %number of rows for matrix A and array B%
for k = 1:12914 %number of rows for column A%
if A(n:3*k) ~= B(n,1)
A(n,3*k) = 0
end
end
end
I have also attached an image of the actual problem where the problem above is described

Accepted Answer

Andrei Bobrov
Andrei Bobrov on 19 Jan 2016
Edited: Andrei Bobrov on 19 Jan 2016
Let your data matrix M:
a = M(:,1:4:end);
b = unique(a(:));
out = zeros(numel(b),size(M,2));
for = jj = 1:size(a,2)
k = (3*jj-2):3*jj;
[lo,ii] = ismember(b,a(:,jj));
out(lo,k) = M(ii(lo),k);
end
  1 Comment
Putsandcalls
Putsandcalls on 20 Jan 2016
Thank you for the help, I also managed to find some useful resources on file exchange as well !

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!