View all functions

CategoryMath: Elementwise
GPUYes

What does the sign function do in MATLAB / RunMat?

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 does the sign function behave in MATLAB / RunMat?

  • Real scalars, vectors, matrices, and higher-dimensional tensors produce -1, 0, or 1 for each element.
  • Complex inputs return x ./ abs(x); zero-valued elements remain exactly 0 + 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) is NaN), matching MATLAB semantics.
  • Inf and -Inf map to 1 and -1 respectively; complex numbers with infinite parts normalise accordingly.

sign Function GPU Execution Behaviour

When RunMat Accelerate is active, tensors that reside on the GPU remain on the device. The runtime checks whether the active provider implements the unary_sign hook:

  • 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.

Examples of using the sign function in MATLAB / RunMat

Determining the sign of a scalar

result = sign(-42);

Expected output:

result = -1;

Applying 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);

S stays on the GPU when the provider implements unary_sign; otherwise RunMat gathers to the host and falls back to the CPU implementation behind the scenes. When sign participates in a fused expression (for example sign(G).*abs(G)), the planner keeps the whole chain device-resident whenever the provider supports the fused kernel.

Handling infinities and NaNs

values = [Inf, -Inf, NaN, 0];
out = sign(values);

Expected output:

out = [1 -1 NaN 0];

GPU residency in RunMat (Do I need gpuArray?)

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.

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.

See Also

abs, sin, sum, gpuArray, gather

Source & Feedback