how can I modify a triple "for" cycle to run faster?

1 回表示 (過去 30 日間)
Hugo
Hugo 2022 年 2 月 15 日
編集済み: Jan 2022 年 2 月 24 日
Hi,
I have the following code, where I fill a 3D matrix with data from a struct, which has tables inside. The goal is to concatenate several 3D matrix. However, they have dfferent number of columns, so I don't think I can concatenate them all directly and skip the code below. The code below is taking me really a long time to compute, like days. My question is: how can I modify my code to make it run faster? I know I can preallocate B as B=zeros(50,10000,30), but I would like to know if there any other options. Thank you.
ntables=50
nlines=10000
ncolumns=30
for i=1:1:ntables
for j=1:1:nlines
for k=1:1:ncolumns
B(i,j,k)=table2array(A((i)).data.A_data(j,k))
end
end
end

採用された回答

Jan
Jan 2022 年 2 月 15 日
編集済み: Jan 2022 年 2 月 24 日
Of course you have to pre-allocate.
ntables = 50;
nlines = 10000;
ncolumns = 30;
B = zeros(ntables, nlines, ncolumns);
for i = 1:ntables
B(i, :, :) = table2array(A(i).data.A_data(:, 1:ncolumns));
end

その他の回答 (0 件)

カテゴリ

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