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 个评论
Margherita Premoli
I is the inventory at time t of batch b
t is the time in days (t=1:T and T=365)
This part of the code in insiede another for loop for t=1:T
and D(t) is the total demand of one day

请先登录,再进行评论。

回答(2 个)

Shashwat Bajpai
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
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.
Margherita Premoli
Margherita Premoli 2020-2-11
Thanks, at least I understood why it happens!
The problem is that I do not know how to pre-allocate Dmet because demand met of day 1 ca be met only with batch 1, demand of day 2 with batches 1 and 2, ecc up to day 9 when all the batches have some products in them. From day 9 on, all the batches can be used to met the demand.
How can tell this to Matlab?
Thanks again for your time

请先登录,再进行评论。


Shashwat Bajpai
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
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.
Margherita Premoli
Margherita Premoli 2020-2-13
Okay, so I do not have to transpose the D right? I mean, should I delete D=D.' ?
Maybe I didn't explain well the I because it is a variable that is defined as the number of products in stock in every batch b for every day t, therefore I thought that the variable I should be written as I(t,b). The same is for Dmet. That is why I transposed the D, so as I will have a matrix with the column vector D (365x1 which means one demand for every day) and the row vector I (1x9 which means that every day there are 9 possible batches). Am I wrong? Should I define a b row vector next to the D column vector and then ask for I(t,b)?
Thansk a lot again! I really appreciate your help

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Resizing and Reshaping Matrices 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by