How can I code this more effectively regarding running time?

I have this data: 'randoms' (cell of cells array size 4319*100) that is: (4319 columns)
my code runs, but it takes 7 min! is there any way to make it more efficient?
this is my code:
for j=1:length(randoms)
all_100=randoms{j};
for l=1:100
randi=all_100{l};
dimers = dimercount(randi);
for k=1:16
x=cell2mat(struct2cell(dimers));
B{l}=x.';
C=cell2mat(B);
S(j)=mean(C(:,k));
end
end
end
Thank you!!!

1 件のコメント

the cyclist
the cyclist 2022 年 4 月 9 日
Generic advice would be to use the profile command, to see which lines of code are taking the most time, and focusing on those.

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

回答 (1 件)

Jan
Jan 2022 年 4 月 9 日

0 投票

It is hard to impossible to optimize code without having the complete code and the input. You have to guess too many details:
  • Are B and S pre-allocated?
  • Why do you overwrite S(j) 1600 times?
  • What is the purpose of struct2cell->cell2mat->cell->cell2mat? This looks far to complicated. Why don't you work with numerical arrays directly?
  • Why do you create the complete matrix C, if you need the k'th column only?

カテゴリ

ヘルプ センター および File ExchangeRandom Number Generation についてさらに検索

質問済み:

2022 年 4 月 9 日

回答済み:

Jan
2022 年 4 月 9 日

Community Treasure Hunt

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

Start Hunting!

Translated by