hhh
hhh

Reputation: 52840

Matlab: command check whether positive or negative sign before a term?

I am looking a command that would do:

a*b*c -----> +

-a --------> -

a*b -------> +

c*d*e*f*a--> +

where a, b, c, d, e and f are symbolic variables in Matlab.

Is there any command to return the initial sign of an expression?

Upvotes: 0

Views: 1755

Answers (1)

lz.
lz.

Reputation: 331

If you have Matlab 2013 you can do this:

>> syms a b c       
>> children(a*b*c)

ans =

[ a, b, c]

>> children(-a)   

ans =

[ a, -1]

>> children(a*b)

ans =

[ a, b]

>> children(-a*-b)

ans =

[ a, b]

>> children(-a*-b*-c)

ans =

[ a, b, c, -1]

You will get the initial sign by looking at the the last element of the returned vector. So test for that.

If you define a variable to a value the result will look like this:

>> c = -4;
>> children(-a*-b*-c)

ans =

[ a, b, 4]

Calling sign on the last element will give -1 if negative, 1 for positive.

Note that there may not always be a numeric value as the last element! Sign(a) will give sign(a) so you will need to assume it is positive in that case.

Upvotes: 2

Related Questions