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

1 vue (au cours des 30 derniers jours)
Hugo
Hugo le 15 Fév 2022
Modifié(e) : Jan le 24 Fév 2022
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

Réponse acceptée

Jan
Jan le 15 Fév 2022
Modifié(e) : Jan le 24 Fév 2022
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

Plus de réponses (0)

Catégories

En savoir plus sur Introduction to Installation and Licensing dans Help Center et File Exchange

Produits


Version

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by