MATLAB Answers

How i can combine three or more than three matrix?

4 ビュー (過去 30 日間)
Akash Pal
Akash Pal 2021 年 8 月 10 日
編集済み: Stephen 2021 年 8 月 10 日
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 ?

採用された回答

Stephen
Stephen 2021 年 8 月 10 日
編集済み: Stephen 2021 年 8 月 10 日
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

その他の回答 (1 件)

KSSV
KSSV 2021 年 8 月 10 日
編集済み: KSSV 2021 年 8 月 10 日
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 件のコメント
KSSV
KSSV 2021 年 8 月 10 日
Store the matrices into a cell and use cat.

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

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by