How to put vector in

1 view (last 30 days)
Imad Boucetta
Imad Boucetta on 17 May 2015
Commented: Star Strider on 17 May 2015
I'm trying to make sign function that return sign of the input the problem is i can't make it return a vector when i type signt(4) i get the resultat but when i put a matrix i get an error
I'm trying to make sign function that return sign of the input the problem is i can't make it return a vector when i type signt(4) i get the resultat but when i put a matrix i get an error
function y = signt(x)
n=length(x);
i=0;
while(i<n);
if x<0
y(i)=-1;
else
y(i)=1;
end
i=i+1;
end
end
end

Accepted Answer

Star Strider
Star Strider on 17 May 2015
Begin ‘i’ at 1 and subscript ‘x’:
n=length(x);
i=1;
while(i<n);
if x(i)<0
y(i)=-1;
else
y(i)=1;
end
i=i+1;
end
  2 Comments
Imad Boucetta
Imad Boucetta on 17 May 2015
Edited: Imad Boucetta on 17 May 2015
Thank u sir but i get a :
Error in ==> singt at 2
n=length(x)
Star Strider
Star Strider on 17 May 2015
Change it to:
n=length(x);
i=0;
while(i<n);
i=i+1;
if x(i)<0
y(i)=-1;
else
y(i)=1;
end
end
That works for vectors and scalars.

Sign in to comment.

More Answers (0)

Categories

Find more on Programming in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!