sign

Sign of each element: -1, 0 or 1.

Category: Mathematics > Elementary Math

Syntax

Y = sign(X)

Returns -1 for negative, 0 for zero and 1 for positive elements of X.

Description

For real input x == sign(x).*abs(x), so sign isolates the direction of a number from its magnitude. It is handy for implementing piecewise rules, step functions and the sign of a derivative or residual.

For a complex number MATLAB defines the sign as z./abs(z), a unit-length number that points in the same direction.

Input arguments

X
Numeric or logical array.

Output arguments

Y
Array of the same size and class as X (logical input gives double). sign(NaN) is NaN.

Example

Negative, zero, positive

sign([-2.5 0 7 NaN])
sign(int8([-5 0 5]))
Output
ans = 1×4

    -1     0     1   NaN
ans = 1×3 int8 row vector

   -1    0    1

See also

abs floor heaviside cumsum