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 Comment

Generic advice would be to use the profile command, to see which lines of code are taking the most time, and focusing on those.

Sign in to comment.

Answers (1)

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?

Categories

Find more on Random Number Generation in Help Center and File Exchange

Asked:

on 9 Apr 2022

Answered:

Jan
on 9 Apr 2022

Community Treasure Hunt

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

Start Hunting!