필터 지우기
필터 지우기

How to concatenate 3D matrixes effectively/fast

조회 수: 3 (최근 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 CenterFile Exchange에서 Introduction 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