How to concatenate 3D matrixes effectively/fast

5 ビュー (過去 30 日間)
Hugo
Hugo 2022 年 2 月 21 日
コメント済み: DGM 2022 年 2 月 21 日
Hi,
I have 5 matrixes, named M1,M2,M3,M4 and M5. They are all 48*1000*C, with C different in each case. I would like to concatenate M1 to M5, in dimension 3. I have written the following code:
ntables=40
nlines=1000
ncolumns1=10
ncolumns2=15
ncolumns3=20
ncolumns4=25
ncolumns5=30
M1=zeros(ntables, nlines, ncolumns1);
M2=zeros(ntables, nlines, ncolumns1);
M3=zeros(ntables, nlines, ncolumns2);
M4=zeros(ntables, nlines, ncolumns3);
M5=zeros(ntables, nlines, ncolumns4);
for i=1:nlines
dataC1(:,i,:)=cat(3,M1(:,i,:),M2(:,i,:))
dataC2(:,i,:)=cat(3,dataC1(:,i,:),M3(:,i,:))
dataC3(:,i,:)=cat(3,dataC2(:,i,:),M4(:,i,:))
dataC4(:,i,:)=cat(3,dataC3(:,i,:),M5(:,i,:))
end
The problem is that the code above is taking very long to run, like it is running for more than 1 week. Is there a faster and easier way to code this?
I thank you in advance.

採用された回答

DGM
DGM 2022 年 2 月 21 日
編集済み: DGM 2022 年 2 月 21 日
... why not just concatenate them?
ntables=10
nlines=10
ncolumns1=10
ncolumns2=15
ncolumns3=20
ncolumns4=25
ncolumns5=30
M1=zeros(ntables, nlines, ncolumns1);
M2=zeros(ntables, nlines, ncolumns2);
M3=zeros(ntables, nlines, ncolumns3);
M4=zeros(ntables, nlines, ncolumns4);
M5=zeros(ntables, nlines, ncolumns5);
dataC = cat(3,M1,M2,M3,M4,M5);
If there's no need for M1, etc, then there's no need to concatenate at all.
ntables = 10
nlines = 10
ncolumns = [10 15 20 25 30];
dataC = zeros(ntables,nlines,sum(ncolumns));

その他の回答 (1 件)

Hugo
Hugo 2022 年 2 月 21 日
Hi,
Thank you for your reply. However, I am not trying to concatenate null matrixes. Instead, I am pre-allocating it. The matrixes M1....M5 are filled with values, not only 0.
  2 件のコメント
Walter Roberson
Walter Roberson 2022 年 2 月 21 日
編集済み: Walter Roberson 2022 年 2 月 21 日
ntables=10;
nlines=10;
ncolumns1=10;
ncolumns2=15;
ncolumns3=20;
ncolumns4=25;
ncolumns5=30;
%now for some example data, just to illustrate that it works.
%in your real code, you would create your real M1, M2, M3, M4, M5
M1=randi(10,ntables, nlines, ncolumns1);
M2=randi(10,ntables, nlines, ncolumns2);
M3=randi(10,ntables, nlines, ncolumns3);
M4=randi(10,ntables, nlines, ncolumns4);
M5=randi(10,ntables, nlines, ncolumns5);
%The matrixes M1....M5 are filled with values, not only 0.
dataC = cat(3,M1,M2,M3,M4,M5);
whos dataC
Name Size Bytes Class Attributes dataC 10x10x100 80000 double
DGM
DGM 2022 年 2 月 21 日
Then the first example should suffice to concatenate them.

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

カテゴリ

Help Center および File ExchangeIntroduction to Installation and Licensing についてさらに検索

製品


リリース

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by