登录论坛

查看完整版本 : [MATLAB基础] 求帮我看看这个While循环为什么是死循环


wang1234587
2013-06-04, 12:57
已知数据如下
qidian=4; zhongdian=3;
course =

1 2 0 4 1 0 0 0 1 0
been =

2 1
2 3
NaN NaN
1 4
5 1
NaN NaN
NaN NaN
NaN NaN
1 6
NaN NaN

我的程序代码如下
temppoint=zhongdian;
while 2
[m n]=find(been==temppoint);
tem=m;
for i2=tem;
if course(i2)~=0&course(i2)~=temppoint
xxx=been(i2,: );
newpoint=xxx(xxx~=temppoint);
st=[st newpoint];
break;
end
end
if newpoint==qidian
break;
end
temppoint=newpoint;
end

运行的时候是死循环,我检查了很多遍都没看出错误在哪里,求大神帮助!谢了
PS:我这个循环是用来从zhongdian出发,逐个搜索been,看他对应的course是否(既不等于0,也不等于temppoint),直到qidian

wang1234587
2013-06-04, 20:43
哎,还是自己找到原因的,都没人回答。
我来说下吧,

因为tem返回的是列向量
我的while中直接用了一个for i2=tem
这是不对的 并不能让i2依次取到tem中的每个数
i2只能取到tem中的第一个数值
但是如果将tem转换为行向量 就可以了