就像标题所说的那样,我的总数组不会添加从我的任何距离数组中获取的任何值,并且只会返回1.我的总数组也不会识别任何i值并且似乎完全忽略它。
任何帮助都会因为我的建议而受到赞赏。
perms_x = perms(x)
perms_y = perms(y)
lngth = length(x)
fctrl = factorial(lngth)
total = zeros(fctrl) //initializes a zero array using factorial as the number of elements
for i=1:fctrl //loop that goes from 1 to factorial value
for k=1:lngth //loops from the start of the array until the length
distance(k) = sqrt((perms_x(i,k)-perms_x(i, k+1))^2 + (perms_y(i, k+1)-perms_y(i, k+1))^2);
total(i) = distance(k) + sum(i);
end
//adds the value of city 1 to 0
distance_1(i) = sqrt(perms_x(i,1)^2+perms_y(i,1)^2);
//adds the value of city n to 0
distance_n(i) = sqrt(perms_x(i,lngth)^2+perms_y(i,lngth)^2);
//adds both values of city 1 and city n to the current sum
total(i) = total(i) + distance_1(i) + distance_n(i);
end
disp (total)
g=min(total)
答案 0 :(得分:0)
查看perms
的手册页显示perms_x和perms_y的大小为lngth!-by-lngth
。但是,由于您的内循环从1
变为lngth
,因此当您提取lngth+1
列时,Scilab会引发越界错误。
内环应该是
for k=1:lngth-1