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

7 Ansichten (letzte 30 Tage)
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 Kommentar
Stephen23
Stephen23 am 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.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Chunru
Chunru am 16 Jun. 2021
Bearbeitet: Chunru am 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

Weitere Antworten (0)

Kategorien

Mehr zu Creating and Concatenating Matrices finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by