How I can append matrices of different dimensions in another matrix?

10 ビュー (過去 30 日間)
SANDEEP SINGH RANA
SANDEEP SINGH RANA 2021 年 5 月 14 日
コメント済み: SANDEEP SINGH RANA 2021 年 5 月 14 日
I want to make a empty matrix in which i want to append matrix of dimensions 3*1,4*1, 3*1,2*1 etc.
It is better if I am able to append through for loop because I have 8-10 matrix of dimensions 3*1,4*1, 3*1,2*1).
Please suggest by showing or refering example.
Thanks
  2 件のコメント
David Fletcher
David Fletcher 2021 年 5 月 14 日
Fundamentally, I suspect what you want to do isn't possible (at least not with a matrix) - you will need to use a cell array see here:
SANDEEP SINGH RANA
SANDEEP SINGH RANA 2021 年 5 月 14 日
Okay, thanks

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

採用された回答

Stephan
Stephan 2021 年 5 月 14 日
A = randi(10, 3, 1)
A = 3×1
2 1 6
B = rand(4, 1)
B = 4×1
0.2924 0.8584 0.9733 0.8157
C = randi(10,5,1)
C = 5×1
2 3 4 3 2
D = vertcat(A,B,C)
D = 12×1
2.0000 1.0000 6.0000 0.2924 0.8584 0.9733 0.8157 2.0000 3.0000 4.0000
  2 件のコメント
SANDEEP SINGH RANA
SANDEEP SINGH RANA 2021 年 5 月 14 日
編集済み: SANDEEP SINGH RANA 2021 年 5 月 14 日
Hi stephan,
Thanks for the help. Can you also tell me how to concatenate these using horzcat. The reason for using horzcat is that when i will call this value (say D), it difficult to specifyor distinguish value of A, B and C matrix. While using horzcat, it showing error. Please help me to overcome this issue.
D = horzcat(A,B,C);
Error using horzcat
Dimensions of arrays being concatenated are not consistent.
Stephan
Stephan 2021 年 5 月 14 日
編集済み: Stephan 2021 年 5 月 14 日
I dont recommend to do so, because you have to transpose them additionally, which doesnt make much sense. If you need a row vector you could also transpose the result of my code instead of transposing all the elements and then use horzcat:
A = randi(10, 3, 1);
B = rand(4, 1);
C = randi(10,5,1);
D = (vertcat(A,B,C)).'
D = 1×12
5.0000 5.0000 7.0000 0.1645 0.1534 0.2924 0.6751 10.0000 2.0000 2.0000 6.0000 6.0000
Maybe you might want to use a cell array:
D = {A, B, C}
D = 1×3 cell array
{3×1 double} {4×1 double} {5×1 double}
There are a many ways to do what you want.

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

その他の回答 (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