Selecting positive or negative terms of a symbolic vector

3 views (last 30 days)
I have a symbolic vector say [a^2*b^3, 0, -a*b^2, b^2, -a^4*b]. I would like to obtain two vectors from this. One of them only selecting the entries that have a positive coefficient, in this case I would like to obtain [a^2*b^3, b^2]. The other containing the entries with negative coefficient: [-a*b^2, -a^4*b].
I tried extracting the coefficients and then using if statement to create a vector with the positive entries and similar for the negative.
Is there a more efficient way to do this?

Accepted Answer

Star Strider
Star Strider on 23 Jun 2015
This works:
syms a b c
V = [a^2*b^3, 0, -a*b^2, b^2, -a^4*b];
Vs = sign(V);
Vgz = V(Vs>0)
Vlz = V(Vs<0)
Vgz =
[ a^2*b^3, b^2]
Vlz =
[ -a*b^2, -a^4*b]
  4 Comments
Star Strider
Star Strider on 24 Jun 2015
My pleasure!
(Please Accept my Answer if it solved your problem.)

Sign in to comment.

More Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!