Checking if sides of triangle are positive and if equality rule is satisfied.

So, I could easily do this using multiple if statements but that seems fairly repetitive. Is there an and or operator in MATLAB so that I could say: if a <= 0 and or b <= 0 and or c <= 0 error... end
And, then do a similar thing with the equality. Sorry for the fairly simple questions, first week or so using MATLAB.

 Accepted Answer

if any([a,b,c]<=0)

9 Comments

Is there a vectorized command that compares the corresponding entries of vectors? Like, checking a matrix of [a + b, a + c, b + c] against one of [ c, b, a]? So that I can check that position 1 of matrix 1 is >= position 1 of matrix 2.
All of the relational operators in MATLAB except for the short circuited ones are vectorized, e.g.,
>> [1,2,3]<=[5, 0, 6]
ans =
1 0 1
If only one of them evaluates to true will the code in the if statement run? Or do all have to evaluate to true?
Which if statement are you talking about? In the code I proposed for you, I use the any() command. As you can see if you read
>> doc any
the output of any(X) is true if any X(i) is true.
Ok, I may have misunderstood so. Does the following code make sense?
function [a, b, c] = Heron(A, S)
if any([a, b, c]) <= 0 || [a + b, a + c, b + c] <= [c, b, a]
error('Invalid selection of a, b or c or invalid combination of the three')
end
Thanks.
Only the if statement obviously. Is there something in particular that's incorrect about it or is it just off the walls in general? Apologies for all the questions.
I can't tell what it's attempting to test. That's what I meant when I said it doesn't make sense.
Aside from that, though, if you run it, you will get an error. The short-circuited logical operators "&&" and "||" only apply to scalar expressions, not vectors.
Gotcha. Just looked up the any()operation and I understand it now. Thanks.

Sign in to comment.

More Answers (1)

if a<=0 || b<=0 || c<=0
% It's bad
end

4 Comments

This evaluates to true if more than one of the variables are less than 0? I was always under the impression that meant or exclusively, like 1 or the other but not both. Thanks.
Btw, the above is a genuine question, not me trying to disprove you.

Sign in to comment.

Categories

Find more on Argument Definitions in Help Center and File Exchange

Asked:

on 2 Oct 2013

Commented:

on 2 Oct 2013

Community Treasure Hunt

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

Start Hunting!