Asked by Sarah
on 2 Feb 2012

Hello everyone,

I have a very simple question....and I have been working on it for some time but cannot figure it out. This is essentially what I would LIKE to say:

for r = 1:length(FreqSec)-1

if FreqSec(1,r+1) > FreqSec(r)*1.01 "OR" FreqSec(1,r+1) <FreqSec(r)*0.99

LagStart = [FreqSec(1,r) r];

break;

end

end

FreqSec is a vector with lots and lots of values....generally within the range of 0.99 and 1.01, except for in a certain interval. I want to detect the exact index point at which the values start changing from the 0.99 to 1.01 range.

Thanks for the help in advance :)

Answer by Walter Roberson
on 3 Feb 2012

Accepted Answer

if FreqSec(1,r+1) > FreqSec(r)*1.01 | FreqSec(1,r+1) <FreqSec(r)*0.99

or

if FreqSec(1,r+1) > FreqSec(r)*1.01 || FreqSec(1,r+1) <FreqSec(r)*0.99

The first of these is more general. The second of these, , is the short-circuiting OR that does not bother to evaluate the second expression if it already knows the final result after the first operation. The operator can only be used between expressions that produce scalar outputs.

Harry MacDowel
on 23 Nov 2013

good clarification on the difference between | and !

Sign in to comment.

Answer by Geoff
on 3 Feb 2012

So you want the last index within the valid range?

I don't know why you are multiplying by 1.01 and 0.99. Perhaps you have described the problem incorrectly or that is the cause of your difficulties. What I think you are trying to do is this:

idx = find( FreqSec >= 0.99 & FreqSec <= 1.01, 1, 'last' )

LagStart = [FreqSec(1,idx) idx];

Or, since it's symmetric:

idx = find( abs(FreqSec-1) <= 0.01, 1, 'last' )

If instead you want the index of the first out-of-range value, use:

idx = find( abs(FreqSec-1) > 0.01, 1, 'first' )

The parameter 'first' is optional, but good for clarity.

-g-

Opportunities for recent engineering grads.

Apply Today
## 1 Comment

## Aaron Eldridge (view profile)

## Direct link to this comment

https://ww2.mathworks.cn/matlabcentral/answers/27899-or-statement-using-if-operator#comment_362106

Sign in to comment.