Matlab unit step function

Status
Not open for further replies.

chris414

New Member
I'm just starting with matlab and need to create a unit step function (I've heard Matlab has the "heaviside" function for this built in, but for the life of me I can't find it). Is this an acceptable way to code the unit step function?

Code:
function [r] = heaviside(x)
    s = size(x);
    r = 1:1:s(2);

    for n=1:s(2)

        if(x(n) >= 0)

            r(n) = 1.0;

        elseif (x(n) < 0)

            r(n) = 0.0;

        else
            
            r(n) = 3.0;
        
        end
        
    end
end
 
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…