View all functions

CategoryMath: Rounding
GPUYes

What does the fix function do in MATLAB / RunMat?

fix(X) removes the fractional part of each element in X by rounding toward zero. Positive numbers behave like floor, negatives behave like ceil, and zeros stay zero. The operation works element-wise on scalars, vectors, matrices, N-D tensors, and complex values.

How does the fix function behave in MATLAB / RunMat?

  • fix rounds positive inputs down toward zero and negative inputs up toward zero.
  • Integer-valued elements are returned unchanged. Logical inputs are promoted to double before rounding.
  • Complex numbers are rounded component-wise (fix(a + bi) = fix(a) + i·fix(b)), matching MATLAB.
  • NaN, Inf, and -Inf propagate unchanged.
  • Character arrays are treated as their numeric code points and return dense double tensors.
  • Empty arrays return empty arrays with identical shape.

fix Function GPU Execution Behaviour

When a tensor already resides on the GPU, RunMat Accelerate checks whether the active provider implements the optional unary_fix hook. If available, the truncation is executed directly on the device and the result stays on the GPU. If the hook is missing, RunMat gathers the values to the host, applies the CPU implementation, and returns a host-resident result—ensuring MATLAB-compatible semantics everywhere.

Examples of using the fix function in MATLAB / RunMat

Truncating positive and negative values toward zero

values = [-3.7 -2.4 -0.6 0 0.6 2.4 3.7];
truncated = fix(values);

Expected output:

truncated = [-3 -2 0 0 0 2 3];

Removing fractional parts from a matrix

A = [1.9  4.1; -2.8  0.5];
B = fix(A);

Expected output:

B = [1 4; -2 0];

Keeping GPU residency when kernels are available

G = gpuArray(linspace(-2.4, 2.4, 6));
truncGpu = fix(G);
hostValues = gather(truncGpu);

Expected output:

hostValues = [-2 -1 0 0 1 2];

Dropping fractional parts of complex numbers

z = [1.9 + 2.6i, -3.4 - 0.2i];
fixed = fix(z);

Expected output:

fixed = [1 + 2i, -3 + 0i];

Converting character arrays to truncated numeric codes

letters = ['A' 'B' 'C'];
codes = fix(letters);

Expected output:

codes = [65 66 67];

GPU residency in RunMat (Do I need gpuArray?)

You usually do not need to call gpuArray manually. RunMat's planner keeps tensors on the GPU when the provider exposes the needed kernels and it is profitable to do so. If the provider does not yet implement unary_fix, RunMat automatically gathers tensors to the host, applies fix, and returns the result. You can still use gpuArray explicitly for compatibility with MATLAB scripts or to force GPU residency.

FAQ

How is fix different from floor and ceil?

fix always rounds toward zero. Positive numbers behave like floor, negatives behave like ceil. Use floor or ceil when you specifically want to round toward negative or positive infinity.

What happens to existing integers?

Integer-valued inputs (including logical values) are returned unchanged; the output is still reported as a double tensor, matching MATLAB's default numeric type.

Does fix change NaN or Inf values?

No. NaN, Inf, and -Inf propagate unchanged.

How are complex numbers handled?

fix rounds the real and imaginary components independently, exactly like MATLAB.

Will fix stay on the GPU?

Yes, when the active provider implements unary_fix. Otherwise, RunMat gathers to the host and applies the CPU implementation, guaranteeing MATLAB-compatible behaviour.

See Also

floor, ceil, round, gpuArray, gather

Source & Feedback