How to vertically concatenate matrices inside a matrix?

Lets say have 2 matrices (A and B), Both A and B have 2 matrices inside (A1,A2 and B1, B2).
A=1x2
A1=1x20, A2=1x20
A1,1=10x1 A1,2=10x1 A1,3=10x1 A1,4=10x1... and so on until A1,20. Basically 20 columns of 10 rows
A2,1=20x1 A2,2=20x1 A2,3=20x1 A2,4=20x1... and so on until A2,20. Basically 20 columns of 20 rows
B=1x2
B1=1x20, B2=1x20
B1,1=30x1 B1,2=30x1 B1,3=30x1 B1,4=30x1... and so on until B1,30. Basically 20 columns of 30 rows
B2,1=40x1 B2,2=40x1 B2,3=40x1 B2,4=40x1... and so on until B2,30. Basically 20 columns of 40 rows
I would like to have a matrix called M with 2 matrices inside (M1 and M2). So that I have:
M=1x2
M1=1x20
M1,1= 40x1 M1,2=40x1 M1,3=40x1 M1,4=40x1...Basically 20 columns of 40 rows
M2=1x20
M2,1= 60x1 M2,2=60x1 M2,3=60x1 M2,4=60x1...Basically 20 columns of 60 rows
I know it sounds complicated but basically I need to concatenate into 1 matrix with 2 matrices inside of 40x20 and 60x20. Thank you!

 Accepted Answer

You seem to have nested cell arrays, so perhaps this might work:
cellfun(@(a,b)cellfun(@vertcat,a,b,'uni',0),A,B,'uni',0)

5 Comments

This gives me
M=1x2
M1,1=1x20 M1,2=1x20
M2,1=1X20 M2,2=1x20
I need
M=1x2
M1=1x20 M2=1x20
Nicole, it would be so much easier to help you if you used valid matlab syntax and correct terminology
You do not have "2 matrices (A and B)". You have two cell arrays. You access the first element of the cell array A with the syntax:
A{1}
not
A1
Similarly, it would seem that the cells of the cell arrays are themselves cell arrays. You would access the first cell array of the first cell with
A{1}{1}
not
A11
Furthermore, Stephen's answer is correct according to your made up notation:
A = {num2cell(rand(10, 20), 1), num2cell(rand(20, 20), 1)}; %should produce a cell array similar to what you describe
B = {num2cell(rand(30, 20), 1), num2cell(rand(40, 20), 1)};
M = cellfun(@(a, b) cellfun(@vertcat, a, b, 'UniformOutput', false), A, B, 'UniformOutput', false);
>>size(M)
ans =
1 2
>>size(M{1})
ans =
1 20
@Nicole Saldias: if it is not correct, please make a comment, upload some sample data, and tell us eaxctly what the expected output would be.
Excuse my notation I was trying to make my problem a little easier to understand but I understand how it made it more confusing. A is a cell array that contains 2 cell arrays the same way B does. I need to concatenate the first cell array in A with the first cell array in B and subsequently with the rest. All the cell arrays inside A and B have the same number columns and different number of rows, which is why I need to vertically concatenate them. I would also like to sort them with respect to the first column.
The expected output would be to have a cell array "M" that contains 2 cell arrays (A{1}&B{1} combined and A{2}&B{2} combined) each sorted with respect to column 1

Sign in to comment.

More Answers (0)

Categories

Asked:

on 13 Apr 2017

Edited:

on 24 Apr 2017

Community Treasure Hunt

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

Start Hunting!