MATLAB Answers

Index in position 2 exceeds array bounds (must not exceed 1).

6 views (last 30 days)
Margherita Premoli
Margherita Premoli on 6 Feb 2020
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 Comments

Margherita Premoli
Margherita Premoli on 6 Feb 2020
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

Sign in to comment.

Answers (2)

Shashwat Bajpai
Shashwat Bajpai on 10 Feb 2020
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 Comments

Margherita Premoli
Margherita Premoli on 10 Feb 2020
This is the previous part of the code. I think that I is defined as a matrix but I am not sure about Dmet. In case, how should I define it?
Thank you so much.
N=10000;
T=365;
S=67 ;
for run=1:N
I(1,:)=zeros(1,9) ;
%DEMAND
D=gamrnd(49.6,1,[1 T]) ; %365 random numbers with gamma distribution
D=D.';
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(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
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
Shashwat Bajpai
Shashwat Bajpai on 11 Feb 2020
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 on 11 Feb 2020
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

Sign in to comment.


Shashwat Bajpai
Shashwat Bajpai on 12 Feb 2020
Edited: Shashwat Bajpai on 12 Feb 2020
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 Comments

Show 1 older comment
Margherita Premoli
Margherita Premoli on 12 Feb 2020
and sorry, one more thing! Why is defined Dmet=zeros(1 365) instead of Dmet=zeros(365 1) since I transposed the row vector of D into a column vector? Because the variables Dmet and I are both described by (t,b) not (b,t).
I am sorry but I am reallt strugglin gin understanding Matlab language :(
Shashwat Bajpai
Shashwat Bajpai on 13 Feb 2020
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 on 13 Feb 2020
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

Sign in to comment.

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by