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

1 view (last 30 days)
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 ?

Accepted Answer

Bruno Luong
Bruno Luong on 10 Jul 2022
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

More Answers (2)

Abderrahim. B
Abderrahim. B on 10 Jul 2022
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 Comment
Akash Pal
Akash Pal on 10 Jul 2022
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

Sign in to comment.


Dhritishman
Dhritishman on 10 Jul 2022
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

Categories

Find more on Matrices and Arrays in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!