sign — Sign of scalars, vectors, matrices, or N-D tensors with real or complex values.
y = sign(x) returns the sign of each element of x. Real inputs become -1, 0, or 1 depending on their value, while complex inputs are normalised to unit magnitude (x ./ abs(x)).
How sign works in RunMat
- Real scalars, vectors, matrices, and higher-dimensional tensors produce
-1,0, or1for each element. - Complex inputs return
x ./ abs(x); zero-valued elements remain exactly0 + 0i. - Logical inputs are promoted to doubles before applying the sign function.
- Character arrays are treated as their numeric code points and return doubles of the same shape.
- NaN inputs propagate (
sign(NaN)isNaN), matching MATLAB semantics. Infand-Infmap to1and-1respectively; complex numbers with infinite parts normalise accordingly.
How sign runs on the GPU
**Hook available:** The sign is evaluated directly on the device with no host transfers.
**Hook missing or unsupported dtype:** RunMat gathers the tensor, applies the CPU logic (including complex handling), and continues execution transparently.
GPU memory and residency
You usually do **not** need to call gpuArray manually. RunMat's planner tracks residency and keeps tensors on the GPU whenever it is profitable. Explicit gpuArray / gather calls remain available for MATLAB compatibility or interoperability with external GPU code.
Examples
Determining the sign of a scalar
result = sign(-42)Expected output:
result = -1Applying sign to a vector of mixed values
v = [-3 -0.0 0 2 5];
s = sign(v)Expected output:
s = [-1 0 0 1 1]Normalising complex numbers to unit magnitude
z = [3+4i, -1+1i, 0+0i];
u = sign(z)Expected output:
u = [0.6+0.8i, -0.7071+0.7071i, 0]Using sign with character data
codes = sign('RunMat')Expected output:
codes = [1 1 1 1 1 1]Working with logical masks
mask = [false true false; true false true];
numeric = sign(mask)Expected output:
numeric = [0 1 0; 1 0 1]Executing sign on a GPU-resident tensor
G = randn(4096, 4096, 'gpuArray');
S = sign(G)Handling infinities and NaNs
values = [Inf, -Inf, NaN, 0];
out = sign(values)Expected output:
out = [1 -1 NaN 0]FAQ
Does sign modify NaN values?
No. NaN inputs remain NaN, matching IEEE behaviour and MATLAB semantics.
How does sign handle complex zeros?
0 + 0i stays 0 + 0i. Other complex values are scaled to lie on the unit circle (x ./ abs(x)).
What happens for infinite complex components?
If either component is infinite, RunMat returns a direction vector with unit magnitude (e.g., 1 + 0i or ±1/√2 ± 1/√2 i), mirroring MATLAB.
Can I call sign on string arrays?
No. sign accepts numeric, logical, or character arrays. Use double(string) followed by sign if needed.
Does sign allocate a new array?
Yes. The builtin returns a fresh array; downstream fusion may combine operations to reduce allocations.
Is GPU execution numerically identical to CPU?
Results match within the provider's precision (single or double). NaN propagation and zero handling remain consistent between CPU and GPU paths.
Will sign participate in fusion?
Yes. The fusion planner can fold sign into neighbouring elementwise kernels, keeping data on the GPU when possible.
How do I keep results on the GPU?
Avoid gather unless host data is required. RunMat keeps the outputs of fused expressions device-resident when beneficial.
Related functions to explore
These functions work well alongside sign. Each page has runnable examples you can try in the browser.
abs, sin, sum, gpuArray, gather, angle, conj, double, exp, expm1, factorial, gamma, hypot, imag, ldivide, log, log10, log1p, log2, minus, plus, pow2, power, rdivide, real, single, sqrt, times
Open-source implementation
Unlike proprietary runtimes, every RunMat function is open-source. Read exactly how sign works, line by line, in Rust.
- View sign.rs on GitHub
- Learn how the runtime works
- Found a bug? Open an issue with a minimal reproduction.
About RunMat
RunMat is an open-source runtime that executes MATLAB-syntax code — faster, on any GPU, with no license required.
- Simulations that took hours now take minutes. RunMat automatically optimizes your math for GPU execution on Apple, Nvidia, and AMD hardware. No code changes needed.
- Start running code in seconds. Open the browser sandbox or download a single binary. No license server, no IT ticket, no setup.
- A full development environment. GPU-accelerated 2D and 3D plotting, automatic versioning on every save, and a browser IDE you can share with a link.