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

조회 수: 1 (최근 30일)
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 CenterFile Exchange에서 Matrices and Arrays에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by