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

7 vues (au cours des 30 derniers jours)
Jimmy Neutron
Jimmy Neutron le 16 Juin 2021
Commenté : Stephen23 le 16 Juin 2021
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 commentaire
Stephen23
Stephen23 le 16 Juin 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.

Connectez-vous pour commenter.

Réponse acceptée

Chunru
Chunru le 16 Juin 2021
Modifié(e) : Chunru le 16 Juin 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

Plus de réponses (0)

Catégories

En savoir plus sur Creating and Concatenating Matrices dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by