Signum Function Matlab Homework Solution

  • Thread starter Thread starter SteliosVas
  • Start date Start date
  • Tags Tags
    Function Matlab
Click For Summary

Discussion Overview

The discussion revolves around a homework problem involving the signum function in MATLAB, specifically applying it to two vectors to perform operations based on the sign of the elements. The participants explore how to square positive values and take the square root of the absolute values of negative ones, while also addressing plotting the results.

Discussion Character

  • Homework-related
  • Technical explanation
  • Mathematical reasoning

Main Points Raised

  • One participant outlines the problem and expresses uncertainty about how to use the signum function results for further operations.
  • Another participant provides a code snippet that uses logical indexing to separate positive and negative values from the vector and suggests plotting the results.
  • A third participant suggests that combining the results into a single array might be necessary instead of keeping them separate.
  • A later reply shares a modified code example that incorporates the signum function into a logical statement, questioning whether this is a simplification of the original approach.

Areas of Agreement / Disagreement

Participants do not reach a consensus on the best approach to combine results or how to handle plotting with differing vector lengths. Multiple competing views on the implementation remain present.

Contextual Notes

Some participants express uncertainty about the logical structure of their code and the implications of using the signum function, indicating potential limitations in their understanding of MATLAB's logical operations.

Who May Find This Useful

Students or individuals working on MATLAB programming, particularly those dealing with vector operations and conditional logic in coding assignments.

SteliosVas
Messages
70
Reaction score
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
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?
 
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   Reactions: donpacino
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   Reactions: SteliosVas
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?
 

Similar threads

  • · Replies 10 ·
Replies
10
Views
2K
  • · Replies 13 ·
Replies
13
Views
3K
  • · Replies 2 ·
Replies
2
Views
3K
Replies
1
Views
2K
Replies
5
Views
6K
Replies
3
Views
3K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 5 ·
Replies
5
Views
2K
Replies
2
Views
3K