If statement with or condition

2 Ansichten (letzte 30 Tage)
luca
luca am 15 Okt. 2019
Beantwortet: Fabio Freschi am 15 Okt. 2019
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 Kommentare
Rik
Rik am 15 Okt. 2019
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 am 15 Okt. 2019
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

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Fabio Freschi
Fabio Freschi am 15 Okt. 2019
if any(settimana < bdr)
...
else
...
end

Weitere Antworten (0)

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange

Tags

Produkte


Version

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by