sign
Sign function (signum function)
Syntax
Description
Examples
Find Sign Function
Find the sign function of a number.
sign(2)
ans = 1
Find the sign function of the values of a vector.
V = [-11 0 1.5 Inf NaN]; sign(V)
ans = 1×5
-1 0 1 1 NaN
Find the sign function of the values of a matrix.
M = magic(3) - 5; sign(M)
ans = 3×3
1 -1 1
-1 0 1
-1 1 -1
Find the sign function of a complex number.
z = 4 - 3*i; sign(z)
ans = 0.8000 - 0.6000i
Plot Sign Function
Plot the sign function and show its behavior at the zero-crossing. Use eps
to represent values just above and below 0
.
x = [-5 -eps(1) 0 eps(1) 5];
y = sign(x);
plot(x,y)
ylim([-2 2])
grid on
Plot Real and Imaginary Parts of Sign Function
Plot real and imaginary parts of the sign function over and .
First, create a mesh of values over -3 < x < 3
and -3 < y < 3
using meshgrid
. Then create complex numbers from these values using z = x + 1i*y
.
v = -3:0.1:3; [x, y] = meshgrid(v); z = x + 1i*y;
Find the real and imaginary parts of the sign function of z
.
s = sign(z); re = real(s); im = imag(s);
Plot the real and imaginary parts.
surf(x,y,re) title('Real part of sign function') xlabel('x') ylabel('y')
figure(2) surf(x,y,im) title('Imaginary part of sign function') xlabel('x') ylabel('y')
Input Arguments
x
— Input
scalar | vector | matrix | multidimensional array
Input, specified as a scalar, vector, matrix, or multidimensional array.
If an element of x
is NaN
, then
sign
returns NaN
in the
corresponding element of the output.
Data Types: single
| double
| int8
| int16
| int32
| int64
| uint8
| uint16
| uint32
| uint64
| logical
| duration
Complex Number Support: Yes
Extended Capabilities
Tall Arrays
Calculate with arrays that have more rows than fit in memory.
The
sign
function fully supports tall arrays. For more information,
see Tall Arrays.
C/C++ Code Generation
Generate C and C++ code using MATLAB® Coder™.
Thread-Based Environment
Run code in the background using MATLAB® backgroundPool
or accelerate code with Parallel Computing Toolbox™ ThreadPool
.
This function fully supports thread-based environments. For more information, see Run MATLAB Functions in Thread-Based Environment.
GPU Arrays
Accelerate code by running on a graphics processing unit (GPU) using Parallel Computing Toolbox™.
The sign
function
fully supports GPU arrays. To run the function on a GPU, specify the input data as a gpuArray
(Parallel Computing Toolbox). For more information, see Run MATLAB Functions on a GPU (Parallel Computing Toolbox).
Distributed Arrays
Partition large arrays across the combined memory of your cluster using Parallel Computing Toolbox™.
This function fully supports distributed arrays. For more information, see Run MATLAB Functions with Distributed Arrays (Parallel Computing Toolbox).
Version History
Introduced before R2006a
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list:
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)