error in the code

17 次查看(过去 30 天)
qas abd
qas abd 2024-8-1,22:24
编辑: qas abd 2024-8-13,21:12
erroe in string(j,n)=string(k,n);
Index in position 1 exceeds array bounds. Index must not exceed 3.
for k=1:Npop
for j=1:dim
busLocation(j,k)=busd(busLocation(j,k),1);
end
sdf= unique(busLocation(:,k));
while length(sdf)<dim
sdf=randi([2 length(busd(:,1))],dim,1);
for j=1:dim
sdf(j)=busd(sdf(j),1);
end
busLocation(:,k)=sdf;
sdf= unique(busLocation(:,k));
end
end
ndif = [0.28; 0.35; 0.39; 0.35; 0.27; 0.34; 0.40; 0.34; 0.25; 0.32];
for k = 1:Npop
mdif(k)= ndif(k);
end
nsw=length(string);
mpop = round(Npop/4);
for j = 1:mpop
minman = min(mdif(:));
for k = 1:Npop
if mdif(k)== minman
for n=1:nsw
string(j,n)=string(k,n);
end
mdif(k)= 1010;
break;
end
end
end
for j = 1:(Npop-mpop)
for bits=1:nsw
string(mpop +j,bits) = string(j,bits);
end
end ; end

回答(1 个)

Voss
Voss 2024-8-1,22:47
The first time this line is encountered:
string(j,n)=string(k,n);
k is 9, but string only has 3 rows. That's the reason for the error.
  2 个评论
qas abd
qas abd 2024-8-1,23:36
Thank you for your response, even this gives an error
string(j,n)=string(j,n);
Torsten
Torsten 2024-8-1,23:43
编辑:Torsten 2024-8-1,23:47
But this error occurs in the last loop:
% select another 75% of new population
for j = 1:(Npop-mpop)
for bits=1:nsw
string(mpop +j,bits) = string(j,bits);
end
end
Before entering this loop, "string" has size 3x10. But j runs up to 10-3 = 7 ...

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Startup and Shutdown 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by