Transposing blocks of matrices from a bigger initial matrix

4 views (last 30 days)
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

Cedric
Cedric on 25 Apr 2013
Edited: Cedric on 25 Apr 2013
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

More Answers (2)

camtlb
camtlb on 26 Apr 2013
There is one more thing that I need (last one..hopefully). Some of the observations are missing. Since I don't want to delete most of my data because of this, I am trying to replace the missing variables with the mean of the others that exist for the same type of data, in the same year.
For instance, in the matrix above, for the first type of data, second year, values are "2" for firm 1 and "14" for firm 2 (let's say also "26" for firm 3 and "38" for firm 4). If, instead of "14", my observation is "NA", I need to replace it by mean(2,26,38).
Thanks :)
  2 Comments
Cedric
Cedric on 26 Apr 2013
Edited: Cedric on 26 Apr 2013
Accept the answer to the current question in this thread and repost a question; in the mean time, I will find a solution.
Cedric
Cedric on 26 Apr 2013
Edited: Cedric on 26 Apr 2013
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.

Sign in to comment.


camtlb
camtlb on 28 Apr 2013
Sorry for not accepting the answer. Thanks a lot!

Categories

Find more on Creating and Concatenating Matrices 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!