MATLAB: How to put vector in

functionoutput

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

Best Answer

  • 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