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

7 ビュー (過去 30 日間)
Jimmy Neutron
Jimmy Neutron 2021 年 6 月 16 日
コメント済み: Stephen23 2021 年 6 月 16 日
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 件のコメント
Stephen23
Stephen23 2021 年 6 月 16 日
"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.

サインインしてコメントする。

採用された回答

Chunru
Chunru 2021 年 6 月 16 日
編集済み: Chunru 2021 年 6 月 16 日
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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by