“if” statement using “or” operator.

조회 수: 3,542 (최근 30일)
Sarah
Sarah 2012년 2월 2일
댓글: Lewis Waswa 2023년 1월 9일
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 :)
  댓글 수: 1
Aaron Eldridge
Aaron Eldridge 2016년 4월 28일
The "||" counts as an or on matlab

댓글을 달려면 로그인하십시오.

채택된 답변

Walter Roberson
Walter Roberson 2012년 2월 3일
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.
  댓글 수: 3
Maximiliano Barbosa
Maximiliano Barbosa 2022년 12월 1일
Thank you
Lewis Waswa
Lewis Waswa 2023년 1월 9일
A quick question, does this mean that the | evaluattes all the conditions?

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

Geoff
Geoff 2012년 2월 3일
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-
  댓글 수: 1
Geoff
Geoff 2012년 2월 3일
Oops, those statements with 'last' are incorrect. You are probably better off doing the 'first' statement and subtracting 1.

댓글을 달려면 로그인하십시오.


Rehman Tabasum
Rehman Tabasum 2021년 4월 30일
i=5
while i>=-1
pause(1)
fprintf('%d\n',i)
i=i-1
if i==0
break
end
end
this is my code for the start button gui for countdown timer so anyone know how to stop coutdown timer when it is runnnig

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by