各位大侠看看我的matlab程序错在哪里?

来源:百度知道 编辑:UC知道 时间:2024/06/22 15:38:11
这是个判断方案hwfp是否合理的程序,大家看看怎么改,程序老是报错:%Subscript indices must either be real positive integers or logicals.
aa=[3.0000,2.5000,1.2000,1.0000,1.0000,2.5000,0.9000,2.0000,0.4000,2.1000,2.2000,1.1000, 2.4000,1.8000,2.6000];
bb=[4.0000,2.0000,1.5000,1.0000,3.2000,1.8000,0.7000,2.5000,1.3000,4.0000,4.0000,3.0000,2.4000,4.0000,2.0000,1.1000,1.6000,4.0000,3.0000,1.9000];
hwfp=[ 18 18 13 3 6 11 19 19 4 19 19 10 18 19 15 19];
for i=1:15
w=0.0;
k=hwfp(i);
h=bb(k); %检查方案是否合理。
for j=1:15
if hwfp(j)==k
w=w+aa(j); %有处错误%Subscript indices must either be real positive integers or logicals.
end
while h<w
hwfp=zeros(1,15);
break;
end
end
end

问题在于这段程序:
while h<w
hwfp=zeros(1,15);
break;
end

当条件满足时hwfp被赋值为零矩阵,而在下一个循环中,你依然使用他的元素作为bb的脚标(matlab中bb的脚标是从1~n的),自然导致越界出错。