problem in running time of my project
Mostra commenti meno recenti
hi, my project running time make me crazy.
I write a script with only 3 " for " but when I click on run button it takes 166 second to run that.
I try it in other computers and Ihave same problem so I realize that the problem is in my script.
with same inputs my friend write another script and we both get same output but his script ran in 3 seconds and my script runs in 166 seconds. (why?) (we have same laptops)
my laptop's specs :
cpu = corei7 5500U
ram = 8 GB
here is my code please tell me where is the problem that I cant get answer in few seconds.
clc
clear
tic;
Input = [ 5 12 0.02
4 20 0.1
6 50 0.01
4 76 0.02
3 100 0.04
4 155 0.04
3 197 0.05
1 350 0.08
2 400 0.12];
n=Input(:,1);
cap=Input(:,2);
FOR=Input(:,3);
COPT=zeros;
copt1=zeros;
Prob=zeros;
%copt 1
for i=0:n(1)
copt1(i+1,1) = cap(1)*i;
copt1(i+1,2) = (nchoosek(n(1),i))*(FOR(1)^(i))*((1-FOR(1))^(n(1)-i));
end
l=1;
for i=1:size(copt1,1)
COPT(l,1)=copt1(i,1);
COPT(l,2)=copt1(i,2);
l=l+1;
end
%problem is starts from here
for i=2:size(cap,1)
a=size(COPT,1);
for j=1:n(i)
for k=1:a
COPT(l,1)=cap(i)*j + COPT(k,1);
l=l+1;
end
end
end
toc;
2 Commenti
Shubham Gupta
il 7 Nov 2019
Modificato: Shubham Gupta
il 7 Nov 2019
What was the output that you were expecting? Is it an array of dimesion (504000 x 2)? Did your friend also get the same anwer? Major problem with the last loop is variable 'a' is changing inside the loop and making COPT bigger and bigger. I am not sure if that's what you intended to do ? Why I think that is because, you haven't defined "l" before the start of your third loop and you are using same "l" that you get from second loop.
f4r3in
il 7 Nov 2019
Risposta accettata
Più risposte (0)
Categorie
Scopri di più su Logical in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!