evaluate whether a condition is met

Hi all, I have to evaluate whether a condition is met: this is the basic method:
if a1 < b1 < a2
disp ('ok')
else disp ('error')
end
If I had n "b" and n+1 "a" I should write n if or is there a way to simplify it? if so how? thank you very much

1 件のコメント

Matt J
Matt J 2012 年 11 月 27 日
編集済み: Matt J 2012 年 11 月 27 日
I assume you really mean
if a1 < b1 & b1 < a2
disp ('ok')
else disp ('error')
end

サインインしてコメントする。

 採用された回答

Matt J
Matt J 2012 年 11 月 27 日
編集済み: Matt J 2012 年 11 月 27 日

0 投票

Assuming size(a) is [1,n+1] and size(b) is [1,n]
if a(1:end-1)<b & b<a(2:end)

その他の回答 (2 件)

Max
Max 2012 年 11 月 27 日

0 投票

Perfect! Now, for example, If during the second cycle the condition is not satisfied, the overall result will be error; it is possible to print statements that do not meet this condition?
I hope I was clear ;)
Max

1 件のコメント

Matt J
Matt J 2012 年 11 月 27 日
disp(find(~(a(1:end-1)<b & b<a(2:end))))

サインインしてコメントする。

Max
Max 2012 年 11 月 27 日

0 投票

Sorry Matt J, I read only now you precondition that
a1 < b1 & b1 < a2
in my situation I have to evaluate if a value is in-range or is out-range. the value have to be in its range
For example:
a_1 < b_1 < a_2
a_2 < b_2 < a_3
a_n < b_n < a_n+1
etc.
and
b_1 = a_1 + x_1
b_2 = a_2 + x_2
b_n = a_n + x_n
etc
x is a column-vector of n elements;
is correct if I do:
if a(1:end-1) < (a(1:end-1) + x(1:end) ) < a(2:end-1)

6 件のコメント

Matt Fig
Matt Fig 2012 年 11 月 27 日
編集済み: Matt Fig 2012 年 11 月 27 日
Please stop posting new "Answers" to your question. None of these are answers to the original question. Post in the comments if you have follow-up questions.
Max
Max 2012 年 11 月 27 日
Sorry...
Matt J
Matt J 2012 年 11 月 27 日
編集済み: Matt J 2012 年 11 月 27 日
is correct if I do: if a(1:end-1) < (a(1:end-1) + x(1:end) ) < a(2:end-1)
No. Triple inequality statements in MATLAB will not do what you expect. That was the point of my earlier Comment. For example, I don't think you expect the following to return false, but it does:
>> 0<.5<1
ans =
0
Contrast this with,
>> (0<.5) & (0.5<1)
ans =
1
Max
Max 2012 年 11 月 28 日
of course, you're right ... I was wrong to do the math...
you're very very talented...
a curiosity ... if, instead, I wanted to print also the statements that verify the condition, which is the command??
thanks for the constant assistance
Max
Matt J
Matt J 2012 年 11 月 28 日
You should post this as a new question and make it clearer. We would probably need a specific example showing the output that you want.
Max
Max 2012 年 11 月 28 日
ok ;)

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeOperating on Diagonal Matrices についてさらに検索

タグ

質問済み:

Max
2012 年 11 月 27 日

Community Treasure Hunt

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

Start Hunting!

Translated by