MATLAB Answers

How i can combine three or more than three matrix?

4 views (last 30 days)
Akash Pal
Akash Pal on 10 Aug 2021
Edited: Stephen on 10 Aug 2021
y=[repmat(x1,size(x2,1),1),repelem(x2,size(x1,1),1)];
I am writing this code for combining two matrix where the row number is different but column is same .
My question is how i can combine more than two matrix where their row number is differnt but column is same ?

Accepted Answer

Stephen
Stephen on 10 Aug 2021
Edited: Stephen on 10 Aug 2021
Note that you will run out of memory very quickly as you increase the number of matrices.
format compact
inp = {rand(3,4),rand(4,4),rand(5,4)}; % all matrices in one cell array.
celldisp(inp)
inp{1} = 0.3652 0.5458 0.7328 0.9886 0.1774 0.4146 0.6908 0.7239 0.5397 0.1625 0.8757 0.0996 inp{2} = 0.4526 0.4298 0.2415 0.8247 0.5996 0.3380 0.2754 0.7322 0.2394 0.6964 0.4858 0.6903 0.1400 0.8696 0.7921 0.3204 inp{3} = 0.3385 0.7161 0.8820 0.6369 0.9931 0.9668 0.0144 0.1382 0.8860 0.5127 0.3950 0.9405 0.0639 0.5230 0.1287 0.5097 0.7254 0.0057 0.9713 0.4787
fun = @(m)1:size(m,1);
idr = cellfun(fun,inp,'uni',0);
[idr{:}] = ndgrid(idr{:}); % comma-separated lists
baz = @(m,r)m(r(:),:);
tmp = cellfun(baz,inp,idr,'uni',0);
out = horzcat(tmp{:})
out = 60×12
0.3652 0.5458 0.7328 0.9886 0.4526 0.4298 0.2415 0.8247 0.3385 0.7161 0.8820 0.6369 0.1774 0.4146 0.6908 0.7239 0.4526 0.4298 0.2415 0.8247 0.3385 0.7161 0.8820 0.6369 0.5397 0.1625 0.8757 0.0996 0.4526 0.4298 0.2415 0.8247 0.3385 0.7161 0.8820 0.6369 0.3652 0.5458 0.7328 0.9886 0.5996 0.3380 0.2754 0.7322 0.3385 0.7161 0.8820 0.6369 0.1774 0.4146 0.6908 0.7239 0.5996 0.3380 0.2754 0.7322 0.3385 0.7161 0.8820 0.6369 0.5397 0.1625 0.8757 0.0996 0.5996 0.3380 0.2754 0.7322 0.3385 0.7161 0.8820 0.6369 0.3652 0.5458 0.7328 0.9886 0.2394 0.6964 0.4858 0.6903 0.3385 0.7161 0.8820 0.6369 0.1774 0.4146 0.6908 0.7239 0.2394 0.6964 0.4858 0.6903 0.3385 0.7161 0.8820 0.6369 0.5397 0.1625 0.8757 0.0996 0.2394 0.6964 0.4858 0.6903 0.3385 0.7161 0.8820 0.6369 0.3652 0.5458 0.7328 0.9886 0.1400 0.8696 0.7921 0.3204 0.3385 0.7161 0.8820 0.6369

More Answers (1)

KSSV
KSSV on 10 Aug 2021
Edited: KSSV on 10 Aug 2021
y=cat(1,repmat(x1,size(x2,1),1),repelem(x2,size(x1,1),1));
Or
y=[repmat(x1,size(x2,1),1) ; repelem(x2,size(x1,1),1)];
  2 Comments
KSSV
KSSV on 10 Aug 2021
Store the matrices into a cell and use cat.

Sign in to comment.

Tags

Community Treasure Hunt

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

Start Hunting!