Drexel dragonThe Math ForumDonate to the Math Forum



Search All of the Math Forum:

Views expressed in these public forums are not endorsed by Drexel University or The Math Forum.


Math Forum » Discussions » Software » comp.soft-sys.matlab

Topic: Checking Values in a For/If loop
Replies: 2   Last Post: Apr 25, 2013 9:34 AM

Advanced Search

Back to Topic List Back to Topic List Jump to Tree View Jump to Tree View   Messages: [ Previous | Next ]
Steven Lord

Posts: 17,944
Registered: 12/7/04
Re: Checking Values in a For/If loop
Posted: Apr 25, 2013 9:34 AM
  Click to see the message monospaced in plain text Plain Text   Click to reply to this topic Reply



"Josh Meyer" <jmeyer@mathworks.com> wrote in message
news:klb9ls$ppj$1@newscl01ah.mathworks.com...
> I can't tell what relation there is between your time(y) vector and the
> other, so I can't really include it here.
>
> j=1;
> for i=2:length(th)
> if th(i)/th(i-1)==.1


This is a Bad Idea; I would compare here with a tolerance or test that
10*th(i) == th(i-1). See question 1 in the Math/Algorithms section of the
newsgroup FAQ for why exact, bit-for-bit equality testing between noninteger
valued floating-point numbers can result in answers you don't expect.

http://matlab.wikia.com/wiki/FAQ

Since you don't change th inside the loop, you can even get rid of the loop
entirely.

th = [100 10 1 0.5 0.25]
A = find(10*th(2:end) == th(1:end-1))+1

*snip*

--
Steve Lord
slord@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com




Point your RSS reader here for a feed of the latest messages in this topic.

[Privacy Policy] [Terms of Use]

© Drexel University 1994-2014. All Rights Reserved.
The Math Forum is a research and educational enterprise of the Drexel University School of Education.