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

5 visualizaciones (últimos 30 días)
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 comentario
Stephen23
Stephen23 el 16 de Jun. de 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.

Iniciar sesión para comentar.

Respuesta aceptada

Chunru
Chunru el 16 de Jun. de 2021
Editada: Chunru el 16 de Jun. de 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

Más respuestas (0)

Categorías

Más información sobre Creating and Concatenating Matrices en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by