while running a matlab program it shows error Subscript indices must either be real positive integers or logicals.

The program is given here
Let NP = population size =10;
D = Number of variables = 3;
pop = [2 4 6; 8 10 12; 5 10 15, 10 20 30; 33 22 11;3 6 9; 5 15 25; 12 24 30; 4 8 12; 8 16 20];
y is the population index that corresponds to best fitness
for np=1:NP
r1=round(unifrnd(1,NP));
while (r1==np)
r1=round(unifrnd(1,NP));
end
for j=1:D
newpop(np,j)=0;
newpop(np,j)=pop(np,j)+round(unifrnd(-1,1)*(pop(y,j)-pop(r1,j)));
end
end

9 Comments

I could not run your code because I don't know what y is.
NP = 10;
D = 3;
pop = [2 4 6;
8 10 12;
5 10 15;
10 20 30;
33 22 11;
3 6 9;
5 15 25;
12 24 30;
4 8 12;
8 16 20];
for np=1:NP
r1=round(unifrnd(1,NP));
while (r1==np)
r1=round(unifrnd(1,NP));
end
for j=1:D
newpop(np,j)=0;
newpop(np,j)=pop(np,j)+round(unifrnd(-1,1)*(pop(y,j)-pop(r1,j)));
end
end
y is number between 1, NP that corresponds to best fitness
The following code ran to completion for me, without error.
NP = 10;
D = 3;
y = 10;
pop = [2 4 6;
8 10 12;
5 10 15;
10 20 30;
33 22 11;
3 6 9;
5 15 25;
12 24 30;
4 8 12;
8 16 20];
for np=1:NP
r1=round(unifrnd(1,NP));
while (r1==np)
r1=round(unifrnd(1,NP));
end
for j=1:D
newpop(np,j)=0;
newpop(np,j)=pop(np,j)+round(unifrnd(-1,1)*(pop(y,j)-pop(r1,j)));
end
end
Can you post a self-contained piece of code that actually gives the error? (Just make a comment here. Don't post a whole new question again.)
r1=round(unifrnd(1,NP));
while (r1==np)
r1=round(unifrnd(1,NP));
end
for j=1:D
F = unifrnd(-1,1)
newpop(np,j)=0;
newpop(np,j)=round(pop(np,j)+(F*(pop(y,j)-pop(r1,j))));
if newpop(np,j)<xmin(j)
newpop(np,j)=xmin(j);
elseif newpop(np,j)>xmax(j)
newpop(np,j)=xmax(j);
end
end
That code does not run for me, because "np" is not defined. Also, "xmin" is not defined.
Again, can you please try to construct a piece of code that we can just paste into a mat files and run, that will show the error?
Sir thank you for your efforts. Please wait I will write the code and send it to you
clear all
clc
D=22;
NP = 50;
R = [150 150 150 150 90 90 95 150 250 310 91 150 150 150 220 220 150 150 220 220 240 240];
for i=1:30;
d(i)=0;
d(i) = rand * (2000);
end
a=[7 7.0 7.0 7.0 6.0 6.0 6.8 7.2 5.25 5.1 6.2 7 7 7 8 8 6 6 8 8 8 8];
b=[8.00 8.00 8.00 8.00 8.00 8.00 8.00 8.00 7.00 7.20 8.25 8.15 8.00 8.00 7.90 7.95 8.18 8.17 7.90 7.95 7.40 7.42];
c=[0.0585 0.0580 0.080 0.0580 0.0610 0.0610 0.0579 0.0565 0.0120 0.0142 0.0600 0.0578 0.0580 0.0585 0.0460 0.0464 0.0570 0.0572 0.0463 0.0462 0.0410 0.0415];
M=[6 5 5 5 6 4 3 4 5 12 4 8 3 6 5 6 5 8 3 3 3 10];
L=[25 26 26 26 25 27 28 27 26 19 27 23 28 25 26 25 26 23 28 28 28 21];
E=[1 1 1 1 1 1 1 1 12 6 1 1 1 1 1 1 1 1 1 1 1 1];
itermax = 1500;
for np = 1:NP
for j=1:D
pop(np,j)=0;
pop(np,j)=round(E(j)+rand(1)*(L(j)-E(j)));
end
end
pop
for np=1:NP
for j=1:D
for k=1:30
x(j,k)=0;
end
end
for j=1:D
temp = pop(np,j);
x(j,temp)=1;
end
for i=1:22 %unit
for j=1:30 %week
x1(i,j)=0;
end
end
for i=1:22
kx(i)=0;
for j=1:30
if(x(i,j)==1)
kx(i)=j;
if(kx(i)>L(i))
kx(i)=L(i);
end
if(kx(i)<E(i))
kx(i)=E(i);
end
break
end
end
if(kx(i)==0) k=round(unifrnd(E(i),L(i)));
else k=kx(i);
end
for j=1:30
if((j<k)||(j>k+M(i)-1))
x1(i,j)=0;
end
if((j>=k)&&(j<=k+M(i)-1))
x1(i,j)=1;
end
end
end
for i=1:22
for j=1:30
if(x1(i,j)==1)
pop(np,i)=j-M(i)+1;
end
end
end
cost(np,1)=0;
for j=1:30
clear z y
x2=x1(:,j);
[z y]=ED(j,x2);
for i=1:22
p(i,j)=y(i);
end
cost(np,1)=cost(np,1)+z;
end
x1;
p;
cost;
end
for iter=1:itermax
[mincost y]=min(cost);
for np=1:NP
r1=round(unifrnd(1,NP));
while (r1==np)
r1=round(unifrnd(1,NP));
end
for j=1:D
newpop(np,j)=0;
newpop(np,j)=pop(np,j)+round(unifrnd(-1,1)*(pop(y,j)-pop(r1,j)));
if newpop(np,j)<E(j)
newpop(np,j)=E(j);
elseif newpop(np,j)>L(j)
newpop(np,j)=L(j);
end
end
for j=1:D
for k=1:30
x(j,k)=0;
end
end
for j=1:D
temp = newpop(np,j);
x(j,temp)=1;
end
for i=1:22 %unit
for j=1:30 %week
x1(i,j)=0;
end
end
for i=1:22
kx(i)=0;
for j=1:30
if(x(i,j)==1)
kx(i)=j;
if(kx(i)>L(i))
kx(i)=L(i);
end
if(kx(i)<E(i))
kx(i)=E(i);
end
break
end
end
if(kx(i)==0) k=round(unifrnd(E(i),L(i)));
else k=kx(i);
end
if(kx(i)==0) k=round(unifrnd(E(i),L(i)));
else k=kx(i);
end
for j=1:30
if((j<k)||(j>k+M(i)-1))
x1(i,j)=0;
end
if((j>=k)&&(j<=k+M(i)-1))
x1(i,j)=1;
end
end
end
for i=1:22
for j=1:30
if(x1(i,j)==1)
newpop(np,i)=j-M(i)+1;
end
end
end
newcost(np,1)=0;
for j=1:30
clear z y
x2=x1(:,j);
[z y]=ED(j,x2);
for i=1:22
p(i,j)=y(i);
end
newcost(np,1)=newcost(np,1)+z;
end
x1;
p;
cost;
end
end
We cannot run this, as ED is not defined. If it is an array then you are attempting to index the array at location 0.
We need to full error message showing exactly which line is triggering the error message for you.

Answers (0)

This question is closed.

Asked:

on 5 Sep 2015

Closed:

on 20 Aug 2021

Community Treasure Hunt

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

Start Hunting!