Index in position 2 exceeds array bounds (must not exceed 1).
2 次查看(过去 30 天)
显示 更早的评论
Hi,
this is part of my code and I have the problem at this line : Dmet(t,b)= min(I(t,b),D(t)- symsum(Dmet(t,b),a,1,b-1)) .
Maybe it is a stupid thing but I don't know how to fix it. Thanks!
for b=1:9
if b==1
Dmet(t,b)=min(I(t,b),D(t)); %Dmet(t) is the fraction of the total demand met with batch 1
Dleft(t,b)=D(t)-Dmet(t,b); %Dleft(t) is the fraction of the total demand that is not met yet and it will be with batches other than 1
else
Dmet(t,b)= min(I(t,b),D(t)- symsum(Dmet(t,b),a,1,b-1)) ;
Dleft(t,b)=D(t)-symsum(Dmet(t,b),a,1,b);
end
end
回答(2 个)
Shashwat Bajpai
2020-2-10
The problem maybe in the way Dmet is being set in the "if" condition ot the I variable maybe a column vector and thus is not taking b>1. Similarly if Dmet is column vector it won't be able to access (t,b). I would suggest add a breakpoint on the line generating the error and check the dimensions of the variables in use.
Hope this Helps!
3 个评论
Shashwat Bajpai
2020-2-11
编辑:Shashwat Bajpai
2020-2-11
Dmet is being initialized a scalar value and thus you get the error. This is happening because in the if statement minimum of I(t,b) and D(t) is '0'. I would suggest pre-allocating Dmet to the required size so that the error does not occur.
Shashwat Bajpai
2020-2-12
编辑:Shashwat Bajpai
2020-2-12
I've made some changes to the code you sent. Please take a look:
N=10000;
T=365;
S=67 ;
for run=1:N
I(1,:)=zeros(1,365) ;
%DEMAND
D=gamrnd(49.6,1,[1 T]) ; %365 random numbers with gamma distribution
D=D.';
Dmet=zeros(1,365);
syms a;
for t=1:T
%PLACE THE ORDER AT THE BEGINNING OF THE DAY
Q(t)=S-sum(I(t)); %ordered boxes at time t that will end in the batch 1 of the day after
%MEETING THE DEMAND
for b=1:9
if b==1
Dmet(t,b)=min(I(b),D(t)); %Dmet(t) is the fraction of the total demand met with batch 1
Dleft(t,b)=D(t)-Dmet(t,b); %Dleft(t) is the fraction of the total demand that is not met yet
else
Dmet(t,b)= min(I(b),D(t)- symsum(Dmet(t,b),a,1,b-1)) ;
Dleft(t,b)=D(t)-symsum(Dmet(t,b),a,1,b);
end
end
end
end
Since you have initialized I as a row vector you do not need to give to values to access its elements, therfore i changed that. The second for loop runs from t=1:T this will run for 365 times and since you allocated I to be only a 1x9 vector it will run into an error. You also need to define the variable a which i declared as a symbol as you are using symsum. If pre-allocating Dmet doesn't work you can define it as a cell array and convert it back to a matrix after the looping is finally over.
Hope this Helps!
4 个评论
Shashwat Bajpai
2020-2-13
I(1,:)=zeros(1,365) ;
The above statement just pre-allocates the variable so that it doesn't face any issues while execution. Dmet is row vector because the I is a row vector and
min(I(b),D(t))
will give '0' if condition of I is satisfied.
另请参阅
类别
在 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!