If statement with or condition

2 次查看(过去 30 天)
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 中查找有关 Loops and Conditional Statements 的更多信息

标签

产品


版本

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by