Transposing blocks of matrices from a bigger initial matrix
Show older comments
Hello,
I am dealing with data for my thesis and since I am stuck at handling it, I would really appreciate your help.
I have a big matrix that contains data for several firms and years (each firm has its own block) and I need to transpose each of these blocks.
Now it looks like this:
________________________________
(A.I1.1) (A.I1.2) (A.I1.3) (A.I1.4)
(A.I2.1) (A.I2.2) (A.I2.3) (A.I2.4)
(A.I3.1) (A.I3.2) (A.I3.3) (A.I3.4)
________________________________
(B.I1.1) (B.I1.2) (B.I1.3) (B.I1.4)
(B.I2.1) (B.I2.2) (B.I2.3) (B.I2.4)
(B.I3.1) (B.I3.2) (B.I3.3) (B.I3.4)
________________________________
where A and B are the firms, I1-I3 are the data for each firm and 1-4 the years of observations.
Each block (A, B) has to be transposed and then placed below the prior one. I need it in this form:
_________________________
(A.I1.1) (A.I2.1) (A.I3.1)
(A.I1.2) (A.I2.2) (A.I3.2)
(A.I1.3) (A.I2.3) (A.I3.3)
(A.I1.4) (A.I2.4) (A.I3.4)
_________________________
(B.I1.1) (B.I2.1) (B.I3.1)
(B.I1.2) (B.I2.2) (B.I3.2)
(B.I1.3) (B.I2.3) (B.I3.3)
(B.I1.4) (B.I2.4) (B.I3.4)
_________________________
The number of firms (A,B) is aprox. 10 000, 22 years and 40 (I1,I2...) variables downloaded in each case.
Thanks a lot!!!
Accepted Answer
More Answers (2)
camtlb
on 26 Apr 2013
0 votes
2 Comments
Well, I had a few minutes to think about it and here is a solution (but next time accept the solution when someone fully answers a question, and restart new threads for new questions). It has to be executed before the code that transposes.
buffer = reshape(M.', [], nFirms)
for k = 1 : size(buffer, 1)
isn = isnan(buffer(k,:)) ;
buffer(k,isn) = mean(buffer(k,~isn)) ;
end
M_cor = reshape(buffer, nYears, []).' ;
and then you work with M_cor (corrected). You can also do
M = reshape(buffer, nYears, []).' ;
and overwrite the original dataset that contains NaNs with the corrected one. I just wanted to keep them both in my example, so you can compare/verify.
camtlb
on 28 Apr 2013
0 votes
Categories
Find more on Logical 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!