The Math Forum

Search All of the Math Forum:

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

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

Notice: We are no longer accepting new posts, but the forums will continue to be readable.

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: 18,038
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" <> wrote in message
> 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.

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

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


Steve Lord
To contact Technical Support use the Contact Us link on

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

[Privacy Policy] [Terms of Use]

© The Math Forum at NCTM 1994-2018. All Rights Reserved.