If statement with or condition

조회 수: 4 (최근 30일)
luca
luca 2019년 10월 15일
답변: Fabio Freschi 2019년 10월 15일
Hi given the following code
BDR= [175 175 175 175 175 175 175 175 175 175 175 175 175];
SETTIMANA=[190 130 120 140 100 160 175 165 157 140 130 175 140 ];
NEXTpro = [25 60 50 40 30 30 30 35 10 34 23 45 12];
if (SETTIMANA(1) < BDR (1)) | (SETTIMANA(2)< BDR(2)) | (SETTIMANA(3)< BDR(3)) | (SETTIMANA(4)< BDR(4))| (SETTIMANA(5)< BDR(5))| (SETTIMANA(6)< BDR(6))| (SETTIMANA(7)< BDR(7))| (SETTIMANA(8)< BDR(8))| (SETTIMANA(9)< BDR(9)) | (SETTIMANA(10)< BDR (10)) | (SETTIMANA(11)< BDR(11))| (SETTIMANA(12)< BDR(12)) | (SETTIMANA(13)< BDR(13));
Y=NEXTpro
else
Y=NEXTpro
SETT1 = SETTIMANA(1:numel(BDR)); % Equalise Vectors
Y = Y(1:numel(BDR)); % Equalise Vectors
idxy = SETT1 <= BDR; % Logical Index Vecto
Y=BDR - SETT1;
Y=Y.*idxy
end
I cannot understand why the if condition cannot read the or operator in the right way.
The conditin is: if I have a value in SETTIMANA that exceed the value in BDR in the same column, then switch to condition else.
  댓글 수: 2
Rik
Rik 2019년 10월 15일
There is no loop in your code, so any line in your code can only execute once. Your conditional is equivalent to any(SETTIMANA<BDR), is that what you mean?
KALYAN ACHARJYA
KALYAN ACHARJYA 2019년 10월 15일
Its perfectly working as you mentioned
What your condition exactly?
If any element of SETTIMANA< correcponding NEXTpro
%...^...Note here
% do
else
%do
end
or
If all element of SETTIMANA< correcponding NEXTpro
%...^...Note here
% do
else
%do
end
Which one
Also you can use logical indexing directly without mentioning one by one of SETTIMANA and NEXTpro

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

채택된 답변

Fabio Freschi
Fabio Freschi 2019년 10월 15일
if any(settimana < bdr)
...
else
...
end

추가 답변 (0개)

카테고리

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

태그

제품


릴리스

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by