abs — Compute absolute values and complex magnitudes elementwise.
abs(X) returns absolute values for real input and magnitudes for floating complex input, with class-preserving saturating semantics for fixed-width integers.
Syntax
Y = abs(X)Inputs
X- Type
Any- Required
- Yes
- Description
- Real numeric input or floating complex input; logical and character forms are RunMat-only extensions.
Returns
Y- Type
NumericArray- Description
- Absolute value or magnitude.
Errors
RunMat:abs:InvalidInput- When
- Input is not supported real numeric, floating complex, or declared extension data.
- Message
abs: invalid input
RunMat:abs:Internal- When
- Internal tensor conversion/allocation/provider interaction failed.
- Message
abs: internal error
RunMat:abs:TooManyOutputs- When
- More than one output is requested.
- Message
abs: too many output arguments
Behavior
Y = abs(X) computes elementwise absolute values for real input and magnitudes for floating complex input while preserving shape. Complex single and double input returns the corresponding real floating class; typed complex integers are rejected.
All eight real integer classes preserve class and shape. Unsigned values are unchanged; signed values are negated; the signed minimum saturates to the corresponding maximum.
Sparse single and double input retains CSC storage and class. Exact sparse integer storage, logical-to-double input, and character-code input are RunMat compatibility extensions. Strings are unsupported, and duration/table/timetable overloads are not yet implemented.
NaN propagates unchanged under IEEE rules.
GPU execution
Supported providers compute real absolute values or complex magnitudes on the owning device. Complex magnitude returns a real resident tensor with the original shape.
Typed unsupported operations gather through authoritative native storage and restore the result through the exact owner. Integer fallback retains the complete fixed-width class, including 64-bit values. Fusion can combine supported elementwise expressions without changing the value contract.
Examples
Take the absolute value of a scalar
y = abs(-42)Expected output:
y = 42Take absolute values across a vector
v = [-2 -1 0 1 2];
result = abs(v)Expected output:
result = [2 1 0 1 2]Measure complex magnitudes
z = [3+4i 1-1i];
magnitudes = abs(z)Expected output:
magnitudes = [5 1.4142]Take absolute values of provider-resident data
G = gpuArray([-3 4; -5 6]);
positive = abs(G);
host = gather(positive)Expected output:
host = [3 4; 5 6]Take magnitudes of logical values in RunMat mode
mask = logical([0 1 0; 1 0 1]);
numeric = abs(mask)Expected output:
numeric = [0 1 0; 1 0 1]Take magnitudes of character code points in RunMat mode
c = 'ABC';
codes = abs(c)Expected output:
codes = [65 66 67]Use abs in an elementwise expression
x = linspace(-2, 2, 5);
y = abs(x) + x.^2Expected output:
y = [6 2 0 2 6]Using abs with coding agents
Open a RunMat example with live inputs, then ask the agent to explain how abs changes the result.
Run a small abs example, explain the result, then change one input and compare the output.
FAQ
Does abs change NaN?⌄
No. abs(NaN) returns NaN under IEEE arithmetic.
What happens to complex values?⌄
Floating complex input returns sqrt(real(X).^2 + imag(X).^2) in the corresponding real floating class.
Can I call abs on strings?⌄
No. Numeric input is supported; RunMat mode additionally enables logical and character extensions.
Does abs preserve sparse arrays?⌄
Real sparse single and double input retains CSC storage. RunMat mode additionally preserves exact typed sparse integers.
Is GPU execution exact?⌄
Execution follows IEEE behavior in the provider precision. Single-precision providers can differ slightly from CPU double results.
How do I keep the result on the GPU?⌄
Do not call gather unless host data is needed. The planner preserves residency when the provider supports the operation.
Does abs allocate?⌄
It returns a new value; fusion can avoid materializing an intermediate when safe.
Can abs accept logical masks?⌄
Only in RunMat compatibility mode, where logical values become double zero or one.
Related Math functions
Elementwise
angle · besselj · bsxfun · complex · conj · double · eps · erf · erfcinv · exp · expm1 · flintmax · gamma · gammaln · heaviside · hypot · imag · int16 · int32 · int64 · int8 · intmax · intmin · ldivide · log · log10 · log1p · log2 · minus · nextpow2 · plus · pow2 · power · rdivide · real · realmax · realmin · realsqrt · rescale · sign · single · sqrt · times · typecast · uint16 · uint32 · uint64 · uint8
Optim
acceptancesa · annealingboltz · annealingfast · coneprog · crossoverarithmetic · crossoverheuristic · crossoverintermediate · crossoverlaplace · crossoverscattered · crossoversinglepoint · crossovertwopoint · eqnproblem · EquationProblem.solve · EquationProblem.subsasgn · EquationProblem.subsref · evaluate · fgoalattain · fitscalingprop · fitscalingrank · fitscalingshiftlinear · fitscalingtop · fminbnd · fmincon · fminimax · fminsearch · fminunc · fseminf · fsolve · fzero · ga · gacreationlinearfeasible · gacreationnonlinearfeasible · gacreationsobol · gacreationuniform · gacreationuniformint · gaplotbestf · gaplotbestindiv · gaplotdistance · gaplotexpectation · gaplotgenealogy · gaplotmaxconstr · gaplotrange · gaplotscorediversity · gaplotscores · gaplotselection · gaplotstopping · integerConstraint · integral · intlinprog · linprog · lsqcurvefit · lsqlin · LsqlinWarmStart.subsasgn · LsqlinWarmStart.subsref · lsqnonlin · lsqnonneg · mutationadaptfeasible · mutationgaussian · mutationpositivebasis · mutationpower · mutationuniform · optimexpr · optimget · OptimizationConstraint.evaluate · OptimizationConstraint.subsasgn · OptimizationConstraint.subsref · OptimizationExitflag.double · OptimizationExitflag.eq · OptimizationExitflag.ne · OptimizationExitflag.string · OptimizationExpression.acos · OptimizationExpression.acosh · OptimizationExpression.acot · OptimizationExpression.acoth · OptimizationExpression.acsc · OptimizationExpression.acsch · OptimizationExpression.asec · OptimizationExpression.asech · OptimizationExpression.asin · OptimizationExpression.asinh · OptimizationExpression.atan · OptimizationExpression.atanh · OptimizationExpression.cat · OptimizationExpression.cos · OptimizationExpression.cosh · OptimizationExpression.cot · OptimizationExpression.coth · OptimizationExpression.csc · OptimizationExpression.csch · OptimizationExpression.ctranspose · OptimizationExpression.cumprod · OptimizationExpression.cumsum · OptimizationExpression.diag · OptimizationExpression.diff · OptimizationExpression.dot · OptimizationExpression.eq · OptimizationExpression.evaluate · OptimizationExpression.exp · OptimizationExpression.ge · OptimizationExpression.horzcat · OptimizationExpression.ldivide · OptimizationExpression.le · OptimizationExpression.log · OptimizationExpression.mean · OptimizationExpression.minus · OptimizationExpression.mldivide · OptimizationExpression.mpower · OptimizationExpression.mrdivide · OptimizationExpression.mtimes · OptimizationExpression.norm · OptimizationExpression.plus · OptimizationExpression.power · OptimizationExpression.prod · OptimizationExpression.rdivide · OptimizationExpression.reshape · OptimizationExpression.sec · OptimizationExpression.sech · OptimizationExpression.sin · OptimizationExpression.sinh · OptimizationExpression.sqrt · OptimizationExpression.subsasgn · OptimizationExpression.subsref · OptimizationExpression.sum · OptimizationExpression.tan · OptimizationExpression.tanh · OptimizationExpression.times · OptimizationExpression.trace · OptimizationExpression.transpose · OptimizationExpression.uminus · OptimizationExpression.uplus · OptimizationExpression.vertcat · OptimizationExpression.zeros · OptimizationExpression.zerosLike · OptimizationProblem.solve · OptimizationProblem.subsasgn · OptimizationProblem.subsref · OptimizationVariable.subsasgn · OptimizationVariable.subsref · optimoptions · optimplotconstrviolation · optimplotfirstorderopt · optimplotfunccount · optimplotfval · optimplotfvalconstr · optimplotmilp · optimplotstepsize · optimplotx · optimproblem · optimset · optimvar · optimwarmstart · particleswarm · patternsearch · psplotbestf · psplotbestx · psplotfuncount · psplotmaxconstr · psplotmeshsize · pswcreationuniform · pswplotbestf · quad · quadprog · QuadprogWarmStart.subsasgn · QuadprogWarmStart.subsref · saplotbestf · saplotbestx · saplotf · saplotstopping · saplottemperature · saplotx · savemilpsolutions · secondordercone · selectionremainder · selectionroulette · selectionstochunif · selectiontournament · selectionuniform · simulannealbnd · solve · surrogateopt · surrogateoptplot · temperatureboltz · temperatureexp · temperaturefast
Trigonometry
acos · acosd · acosh · acotd · acscd · asecd · asin · asind · asinh · atan · atan2 · atan2d · atand · atanh · cos · cosd · cosh · cospi · deg2rad · pol2cart · rad2deg · sin · sind · sinh · sinpi · tan · tand · tanh
Reduction
all · any · bounds · cummax · cummin · cumprod · cumsum · cumtrapz · diff · gradient · max · maxk · mean · median · min · mink · movmax · movmean · movmedian · movmin · movprod · movstd · movsum · movvar · nnz · prod · rms · std · sum · trapz · var
Structure
bandwidth · isdiag · ishermitian · issymmetric · istril · istriu · symrcm
Signal
blackman · butter · buttord · cheb2ord · conv · conv2 · deconv · downsample · envelope · filter · filtfilt · fir1 · freqz · gauspuls · hamming · hann · hilbert · periodogram · pulstran · pwelch · rectpuls · resample · sawtooth · sinc · spectrogram · square · tripuls · unwrap · upsample · zplane
Factor
chol · decomposition · decomposition.ctranspose · decomposition.mldivide · decomposition.mrdivide · decomposition.mtimes · decomposition.rdivide · decomposition.subsref · decomposition.times · decomposition.uminus · decomposition.uplus · eig · eigs · gmres · ichol · ilu · lu · ordeig · ordqz · ordschur · pcg · qr · qz · rsf2csf · schur · svd
Ops
cross · ctranspose · dot · mldivide · mpower · mrdivide · mtimes · pagemtimes · pagetranspose · trace · transpose
Interpolation
griddedInterpolant · griddedInterpolant.subsref · interp1 · interp1q · interp2 · pchip · ppval · spline
Integer Division
Source you can read
Every RunMat function's source is public. Read exactly how abs is executed, line by line, in Rust.
- View the source for abs in Rust on GitHub
- Learn how the RunMat runtime works
- Found a bug? Open an issue with a minimal reproduction.
About RunMat
RunMat is a runtime that executes MATLAB-syntax code fast on any GPU. The runtime is free for individuals, including freelancers; students and faculty at an academic institution, for teaching or research there; contributors to an open-source project, for work on that project; organizations with 250 or fewer people and US$10M or less in annual revenue; and anyone, for non-production use. See runmat.com/license.
- RunMat automatically optimizes your math for GPU execution on Apple, Nvidia, and AMD hardware. No code changes needed. Simulations that took hours now take minutes.
- Start running code in seconds. RunMat runs in the browser, on the desktop, or from the CLI. No license server, no IT ticket.