How to vertically combine two matrices with a space in-between them?

5 views (last 30 days)
I am working with a ton of the same matrices that I want to save into an excel file with two blank lines of cells separating all of them. In this case I will use three. I tried vertcat, but I get an error that the dimensions are not consistent... I don't understand why it should be a problem if the have the same number of columns...
A = [ 1 2 3 4; 5 6 7 8; 3 2 4 5]
B = [ 6 3 2 1, 5 6 4 6, 7 8 1 2 ]
C = [ 5 4 1 2; 5 9 5 6; 4 1 2 1]
I would like to achieve a single matrix that looks like this:
1 2 3 4
5 6 7 8
3 2 4 5
6 3 2 1
5 6 4 6
7 8 1 2
5 4 1 2
5 9 5 6
4 1 2 1
How would I go about iterating and joining the matrices in a loop?
  1 Comment
Stephen23
Stephen23 on 16 Jun 2021
"I tried vertcat, but I get an error that the dimensions are not consistent... I don't understand why it should be a problem if the have the same number of columns"
They don't have the same number of columns:
B = [ 6 3 2 1, 5 6 4 6, 7 8 1 2 ]
% ^ ^
Actually checking the sizes is much more reliable than relying on what you think/hope their sizes are.

Sign in to comment.

Accepted Answer

Chunru
Chunru on 16 Jun 2021
Edited: Chunru on 16 Jun 2021
You can insert one row of NaN. An array or matrix must be filled with numbers and cannot have blanks (unless you use strings). NaNs are also considered as numbers and they can be put into an array or matrix.
A = [ 1 2 3 4; 5 6 7 8; 3 2 4 5];
B = [ 6 3 2 1; 5 6 4 6; 7 8 1 2 ];
C = [ 5 4 1 2; 5 9 5 6; 4 1 2 1];
D =[A; nan(1,4); B; nan(1,4); C]
D = 11×4
1 2 3 4 5 6 7 8 3 2 4 5 NaN NaN NaN NaN 6 3 2 1 5 6 4 6 7 8 1 2 NaN NaN NaN NaN 5 4 1 2 5 9 5 6

More Answers (0)

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!