Search All of the Math Forum:

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

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

 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

"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