开发者

Simple loop doesn't work

开发者 https://www.devze.com 2023-04-10 13:28 出处:网络
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

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)
  开发者_如何学JAVA  %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!


Are you sure that someArray contains the value 1.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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号