MATLAB: Transposing blocks of matrices from a bigger initial matrix

loopmatrixtranspose

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!!!

Best Answer

So you don't have A, B, C, etc, but a large M that you have to split/transpose/cat?
If I understand well what you want to do, here is a simple method. It might not be the most efficient, but it should not be an issue as your initial array is "reasonably small". Illustration:
>> M = [1 2 3 4; 5 6 7 8; 9 10 11 12; 13 14 15 16; 17 18 19 20; 21 22 23 24]
M =
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
17 18 19 20
21 22 23 24
this is the large matrix, that contains here 2 firms with 3 data per firm over 4 years.
>> nFirms = 2 ;
>> nData = size(M,1) / nFirms ;
>> nYears = size(M,2) ;
>> M_tr = cell2mat(mat2cell(M, nData*ones(nFirms,1), nYears).').'
M_tr =
1 5 9
2 6 10
3 7 11
4 8 12
13 17 21
14 18 22
15 19 23
16 20 24