index out of bounds because numel(c)=2
Show older comments
i am doing an algorithm and i am getting the following error:
??? Attempted to access c(3); index out of bounds because numel(c)=2.
Can some one plz help me in getting rid of that error?
Main algorithm:
Co=0.0 ;
i=0;
While (more jobs to process) {
i++ ;
ai=GetArrival();
if (ai<Ci-1)
di= Ci-1- ai ;
Else
di=0.0 ;
si=GetService() ;
ci<Ci-1)= ai+ di+ si ;
}
n=I;
Return d1, d2, ……dn ;
Code I Have Made
clc
clear
a = [0 15 35 60 85 110 230 345 460 500 620 740 860 990 1020 1045]
s = [0 23 20 40 70 86 100 30 85 105 160 60 120 143 205 160]
m = 1
c(m) = 0
j = 1
while j<15
if a(j+1)<c(j)
d(j)= c(j)-a(j+1)
else
d(j+1)= 0
c(j+1)= a(j+1)+d(j+1)+s(j+1)
end
j = j+1
end
disp('the dlay time for 15 arrivals:')
D = [d(j)]
1 Comment
Andrei Bobrov
on 7 Mar 2012
you have 'a' and 's', what should be 'd'
Answers (2)
Andrei Bobrov
on 6 Mar 2012
d = filter2([0,-1;1,0],[a;a+s],'valid');
d(d < 0) = 0;
your code
a = [0 15 35 60 85 110 230 345 460 500 620 740 860 990 1020 1045];
s = [0 23 20 40 70 86 100 30 85 105 160 60 120 143 205 160];
c = zeros(size(a)-[0 1]);
d = c;
j1 = 1;
while j1 <= 15
if a(j1+1)<c(j1)
d(j1)= c(j1)-a(j1+1);
end
c(j1+1)= a(j1+1)+s(j1+1);
j1 = j1+1;
end
Honglei Chen
on 6 Mar 2012
0 votes
You have an issue with your algorithm. You are growing your c and d at each iteration. When j is 2, it never went into the part where c(j+1) is created, so c(3) is undefined when j is 3 when you compared it to a(j+1).
I don't know how to fix it though because I don't know what you are doing. The trick is to make sure that c(j+1) is defined for each iteration.
Categories
Find more on Loops and Conditional Statements 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!