简单的循环不起作用
这太疯狂了,我已经被这个 MATLAB 代码困住了很多年了。我得到的结果如下:
for i = 1 : 0.1 : 5
index = find(someArray == i)
%do stuff
end
现在循环第一次运行时,当 i = 1 时,index 返回正确的值(例如 45)。现在,第二次运行循环时,索引被设置为一组空括号 - '[]' ...! (其余迭代也会发生同样的情况)。
这很疯狂,因为从 1.1 开始循环将索引设置为逻辑数值。事实上,每次迭代都应该使用 find 为索引设置一个逻辑值。所以就像“查找”不喜欢在循环或其他东西中使用一样......
非常感谢任何想法或帮助!
This is crazy, have been stuck on this MATLAB code for ages. What I've got is the following:
for i = 1 : 0.1 : 5
index = find(someArray == i)
%do stuff
end
Now the first time the loop runs, when i = 1, index returns a correct value (e.g. 45). Now, the second time the loop runs, index gets set to an empty set of brackets - '[]' ...! (The same happens for the rest of the iterations).
This is crazy because starting the loop at 1.1 sets index to a logical numerical value. In fact, every iteration should set a logical value for index using find. So it's like 'find' doesn't like being used inside loops or something....
Any ideas or help is much appreciated!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您确定
someArray
包含值1.1
吗?这可能只是浮点精度的问题。网上有很多关于此类事情的信息,包括:http: //www.mathworks.com/company/newsletters/news_notes/pdf/Fall96Cleve.pdf
http://floating-point-gui.de/
http://www.mathworks.com/support/tech-notes/1100/1108.html
http://www.mathworks.com/help/techdoc/matlab_prog/ f2-12135.html?#bqxyrhp
Are you sure that
someArray
contains the value1.1
? This could simply be a problem with floating point precision. There's lots of information about this sort of thing on the web, including:http://www.mathworks.com/company/newsletters/news_notes/pdf/Fall96Cleve.pdf
http://floating-point-gui.de/
http://www.mathworks.com/support/tech-notes/1100/1108.html
http://www.mathworks.com/help/techdoc/matlab_prog/f2-12135.html?#bqxyrhp