How to create a simple matrix from two different kind of matrix size ?

1 回表示 (過去 30 日間)
Akash Pal
Akash Pal 2022 年 7 月 10 日
回答済み: Dhritishman 2022 年 7 月 10 日
I have a matrix whose size is 2X5X2 double and another matrix whose size is 2X5 double how i can create a matrix from it where there will be two rows and 15 column total ?

採用された回答

Bruno Luong
Bruno Luong 2022 年 7 月 10 日
M1 = rand(2,5,2) ;
M2 = rand(2,5) ;
reshape(cat(3,M1,M2),size(M1,1),[])
ans = 2×15
0.5939 0.9414 0.3576 0.3815 0.2446 0.3603 0.1188 0.0321 0.9936 0.3906 0.6862 0.2377 0.6573 0.5539 0.9097 0.3128 0.6314 0.7702 0.8176 0.9711 0.9955 0.6620 0.7915 0.9256 0.5145 0.1239 0.1524 0.1599 0.9401 0.3216

その他の回答 (2 件)

Abderrahim. B
Abderrahim. B 2022 年 7 月 10 日
Hi !
Try this:
M1 = rand(2,5,2) ;
M2 = rand(2,5) ;
M = [M1(:,:,1) M1(:,:,2) M2] ;
size(M)
ans = 1×2
2 15
Maybe there is a better way to do this.
HTH
  1 件のコメント
Akash Pal
Akash Pal 2022 年 7 月 10 日
this will work if i put the value of M1 manually ,but i want if the size of M1 also changed that time i should get my result autmatically ,not putting the value manually two times

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


Dhritishman
Dhritishman 2022 年 7 月 10 日
You can use the reshape(used in reshaping arrays) and cat(used to concatenate arrays) functions in this case:
mat1 = rand(2, 5, 2) ;
mat2 = rand(2, 5) ;
reshape(cat(3, mat1, mat2), size(mat1, 1), [])
ans = 2×15
0.7267 0.0855 0.6519 0.7025 0.0737 0.9559 0.0360 0.3340 0.9461 0.1479 0.8802 0.3786 0.7842 0.7323 0.7065 0.0695 0.9540 0.6237 0.6092 0.8402 0.8980 0.5996 0.2417 0.9426 0.6486 0.8610 0.5439 0.8975 0.4047 0.5239

カテゴリ

Help Center および File ExchangeMatrices and Arrays についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by