Signum Function Matlab Homework Solution

In summary, the conversation discusses using the signum function on two vectors x and y to perform an operation on the positive and negative values. The final solution involves creating a single array containing all the solutions and using a logical statement to apply the signum function. A code is provided for plotting the solutions on the same graph as the original vectors.
  • #1
SteliosVas
70
0

Homework Statement



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]

Homework Equations



Signum function matlab

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...
 
Physics news on Phys.org
  • #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);

negative2=y(negative);

positive2=y(positive);

while negative2<0;

x2=sqrt(abs(negative2));

if positive2>0;

y2=positive2.^2;

break

end

end

%plot(x,y,x2,y2,'rs')

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?
 
  • #3
SteliosVas said:
negative2=y(negative);

positive2=y(positive);

while negative2<0;

x2=sqrt(abs(negative2));

if positive2>0;

y2=positive2.^2;

break

end
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.
 
  • Like
Likes donpacino
  • #4
SteliosVas, take a look over this code, I hope that it helps a bit.

%clc
%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,
G.P.

 
  • Like
Likes SteliosVas
  • #5
GoodPost said:
Yout(Ytest == 1 )= a(Ytest == 1)
Is this simplifying what I had, in the sense it is putting the signum function into a logical statement?
 

1. What is the Signum Function in Matlab?

The Signum Function, also known as the sign function, is a mathematical function used in Matlab that returns the sign of a number. It returns a value of -1 if the number is negative, 0 if the number is 0, or 1 if the number is positive.

2. How do I use the Signum Function in Matlab?

To use the Signum Function in Matlab, you can simply type "sign(x)" where x is the number you want to find the sign of. Alternatively, you can use the "sgn(x)" command.

3. Can I use the Signum Function with vectors or matrices in Matlab?

Yes, the Signum Function can be applied to vectors and matrices in Matlab, and it will return the sign of each individual element in the vector or matrix.

4. What is the purpose of the Signum Function in Matlab?

The Signum Function is useful in many mathematical and engineering applications, such as signal processing, control systems, and image processing. It can be used to determine the polarity of a signal or the direction of a vector.

5. Are there any other functions similar to the Signum Function in Matlab?

Yes, there are other functions in Matlab that perform similar tasks, such as the "signbit(x)" function, which returns the sign bit of a number, and the "signum(x)" function, which returns the sign and magnitude of a number.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
10
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
13
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
5K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
1K
Back
Top