for i=5:1:97
for j=hmin+2:i:hmax+2
h=[];
h=[h,j];
if(j~=hmax+2)
jp=j+i;
h=[h,jp];
end
disp(h)
p=hmin;
u=1;
while(u<=size(h,2))
hn=[];
while(h(u)-table3(p,1)>=2&&p<=hmax)
p=p+1;
hn=[hn,(h(u)-table3(p,1)-2)*table3(p,2)];
end
u=u+1;
c=sum(hn,2)
end
c=0;
end
end
最外面的for循环没有运行,而且报错显示位置1处的索引超出边界,索引不能超出125(那个hmax是125)
h = []; % 将 h 的定义移至外部循环之前,以保留其值
for i = 5:1:97
h = []; % 将 h 的初始化移至内部循环之前,以清空上一次循环的值
for j = hmin+2:i:hmax+2
h = [h, j];
if j ~= hmax+2
jp = j + i;
h = [h, jp];
end
disp(h);
p = hmin;
u = 1;
while u <= numel(h) % 使用 numel(h) 替代 size(h, 2)
hn = [];
while h(u) - table3(p, 1) >= 2 && p <= hmax % 添加 p <= hmax 的条件判断
p = p + 1;
hn = [hn, (h(u) - table3(p, 1) - 2) * table3(p, 2)];
end
u = u + 1;
c = sum(hn, 2);
disp(c); % 输出 c 的值
end
c = 0;
end
end
案(NaN或Inf),可以先检查数据是否正确,例如是否存在除数为0的情况等。如果数据没问题,可以尝试使用过滤或插值方法来处理 NaN 或 Inf 值,或者考虑使用其他算法来解决问题。