running time problem in matlab
2 次查看(过去 30 天)
显示 更早的评论
again I have running time problem.
I want to write a script that find dublicate rows and keep one of them and sum probability of them then write that probability in the 2nd column next to that row.
for example:
I want to change this input
12 0.02
16 0.04
20 0.01
20 0.015
20 0.01
30 0.05
to this :
12 0.02
16 0.04
20 0.035
30 0.05
I write below script to do this action but it takes too long to do this. (also my friend do this action by this scrip in a few seconds (we have same laptops) )
COPT=sortrows(COPT);
%till here program runs in 0.4 sec
%but from here to end takes too long to run this program.
u=1;
while u<=size(COPT,1)-1
if COPT(u,1)==COPT(u+1,1)
COPT(u,2)= COPT(u,2)+COPT(u+1,2);
COPT(u+1,:)=[];
end
u=u+1;
end
I also upload my input ("COPT.mat") here if you want to use it. (I know my input is huge but as I said my friend run this script in only a few seconds)
what should I do???
0 个评论
回答(1 个)
Steven Lord
2019-11-8
Take a look at groupsummary.
3 个评论
Steven Lord
2019-11-8
groupsummary is a function introduced in MATLAB in release R2018a. If you're using release R2018a or later, you can see what it does and how to use it by reading its documentation page.
doc groupsummary
If you're using an older release, search the MathWorks website for the word "groupsummary." If it looks like it will be useful for your application and you are willing to upgrade to release R2018a or later, upgrade and you'll be able to use it.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Resizing and Reshaping Matrices 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!