1. Limited time only! Sign up for a free 30min personal tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Homework Help: Signum function Matlab

  1. Aug 31, 2015 #1
    1. The problem statement, all variables and given/known data

    Okay basically we have two vectors x and y, and we are required to use the signum function to get the negative and positive ones and preform a operation on them. That is square the positive ones and square root of the absolute value of the negative ones.

    x= [ 1:1:10]

    y= [ 0.841 0.909 0.141 -0.756 -0.958 -0.279 0.656 0.989 0.412 -0.544]

    2. Relevant equations

    Signum function matlab
    3. The attempt at a solution

    I know if i do: sign(y) I will get values of 1 and -1 (zero possible but not in this case)

    so for example -1 if the value is <0 and 1 if value > 0

    The thing is since this is not a logical statement or I don't think so anyway, I don't know how i can utilize those 1's + and -, to preform an operation

    I know if it was logical I could to say value(sign(y)) or use a if statement but not this instance it didn't work...
  2. jcsd
  3. Aug 31, 2015 #2
    x= [ 1:1:10];

    y= [ 0.841 0.909 0.141 -0.756 -0.958 -0.279 0.656 0.989 0.412 -0.544];

    positive = logical(sign(y)==1);

    negative = logical(sign(y)==-1);



    while negative2<0;


    if positive2>0;






    So I got this now.. but know I have to plot it on the samme plot as x,y how would I do that if vector lengths are different?
  4. Aug 31, 2015 #3


    User Avatar

    Staff: Mentor

    My guess is that you are suppose to create a single array containing all the solutions, not two smaller arrays (one for each case) as you do above.
  5. Aug 31, 2015 #4
    SteliosVas, take a look over this code, I hope that it helps a bit.

    %clear all

    x= 1:1:10;
    y= [ 0.841 0.909 0.141 -0.756 -0.958 -0.279 0.656 0.989 0.412 -0.544]
    Ytest = sign(y)
    a = y.^2;
    b = sqrt(abs(y));
    Yout =b;
    Yout(Ytest == 1 )= a(Ytest == 1)

    plot( x, Yout, x, y)

    Best regrads,

  6. Sep 6, 2015 #5

    Is this simplifying what I had, in the sense it is putting the signum function into a logical statement?
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook

Have something to add?
Draft saved Draft deleted