0

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!

Amro
  • 123,847
  • 25
  • 243
  • 454
Jordan Smith
  • 10,310
  • 7
  • 68
  • 114
  • 2
    possible duplicate of [Problem using the find function in MATLAB](http://stackoverflow.com/questions/2669942/problem-using-the-find-function-in-matlab) – Jonas Oct 05 '11 at 11:56

1 Answers1

6

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

Edric
  • 23,676
  • 2
  • 38
  • 40
  • Thanks. I think you saved my sanity. So for example, (0.3 == 0.1*3) evaluates to false... wow, not exactly intuitive. – Jordan Smith Oct 05 '11 at 09:18
  • 1
    Welcome to the wonderful world of digital arithmetic. It ain't the same as you learned in grade school!!! Good luck. – John Oct 05 '11 at 16:41