running time problem in matlab

2 views (last 30 days)
f4r3in
f4r3in on 8 Nov 2019
Commented: f4r3in on 8 Nov 2019
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???

Answers (1)

Steven Lord
Steven Lord on 8 Nov 2019
Take a look at groupsummary.
  3 Comments
Steven Lord
Steven Lord on 8 Nov 2019
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.
f4r3in
f4r3in on 8 Nov 2019
Thanks for answering but I use matlab 2016a and I look at groupsummary, it doesn't do what I want. I write the script that I want but it takes too time to run. I write my problem here to find faster way to do my job.

Sign in to comment.

Categories

Find more on Resizing and Reshaping Matrices in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!