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)
(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
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
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 Exchange 中查找有关 Random Number Generation 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!