abs

Absolute value of real numbers and magnitude of complex numbers.

Category: Mathematics > Elementary Math

Syntax

Y = abs(X)

Returns the absolute value of each element of X.

Description

abs works element by element. For a real number it drops the sign. For a complex number it returns the distance from the origin in the complex plane, which is always real and non-negative.

Integer types saturate instead of overflowing: abs(int8(-128)) is 127, because 128 does not fit in int8. A logical input is converted to double.

Input arguments

X
Numeric or logical array of any size. Complex values are allowed.

Output arguments

Y
Array of the same size as X. For complex input it holds the magnitude sqrt(real(X)^2 + imag(X)^2).

Example

Real and complex values

abs([-3 0 2.5])
abs(3 + 4i)
abs(int8(-128))
Output
ans = 1×3

    3.0000         0    2.5000
ans = 5
ans = int8
   127

See also

sign angle real imag hypot