complex — Construct complex values from real and imaginary components.
complex constructs class-aware complex scalars and arrays, applies scalar component expansion, and preserves supported provider residency.
Syntax
Z = complex(A)
Z = complex(A, B)Inputs
A- Type
Any- Required
- Yes
- Description
- Real numeric input to lift, or an existing complex value to preserve.
A- Type
Any- Required
- Yes
- Description
- Real component.
B- Type
Any- Required
- Yes
- Description
- Imaginary component.
Returns
Z- Type
NumericArray- Description
- Complex result with the selected numeric class and compatible input shape.
Errors
RunMat:complex:TooManyOutputs- When
- More than one output is requested.
- Message
complex: too many output arguments
RunMat:complex:InvalidArgument- When
- Argument arity is invalid.
- Message
complex: invalid argument
RunMat:complex:InvalidInput- When
- An input is not a supported real numeric component, or a binary input is already complex.
- Message
complex: invalid input
Construction and shapes
Z = complex(A, B) constructs A + 1i*B from real numeric components. Two non-scalar components must have the same size; a scalar component expands across the other input. This constructor does not apply general implicit expansion.
Z = complex(A) gives real input an explicit zero imaginary component. Existing complex scalars and arrays pass through unchanged. Empty arrays retain their shape.
Classes
Two floating-point components produce complex single when either component is single and complex double otherwise. Logical components convert to complex double. Strings and character arrays are not numeric constructor inputs.
All eight fixed-width integer classes use exact complex-integer storage. When either binary component is integer, the other must have the same integer class or be a full scalar double. A scalar double is rounded and saturated according to the integer class.
Accelerated execution
Compatible real floating-point gpuArray inputs use the owning provider's complex-construction operation. RunMat validates the returned shape, storage, precision, owner, device, class metadata, and non-aliasing before accepting it.
A typed unsupported result enters owner-aware host fallback. Other provider errors remain visible. Typed-integer values transfer without a floating-point intermediary and return to the selected owner when exact restoration is available.
Examples
Construct a complex scalar
z = complex(3, 4)Expected output:
z = 3+4iCombine matching arrays
re = [1 2 3];
im = [4 5 6];
z = complex(re, im)Expected output:
z = [1+4i 2+5i 3+6i]Expand a scalar component
re = [1 2 3];
z = complex(re, -1)Expected output:
z = [1-1i 2-1i 3-1i]Add an explicit zero imaginary component
z = complex(12);
tf = isreal(z)Expected output:
z = 12+0i
tf = 0Preserve an existing complex value
z = complex(1 + 2i)Expected output:
z = 1+2iConstruct a resident complex array
re = gpuArray([1 2 3]);
im = gpuArray([4 5 6]);
G = complex(re, im);
z = gather(G)Expected output:
z = [1+4i 2+5i 3+6i]Using complex with coding agents
Open a RunMat example with live inputs, then ask the agent to explain how complex changes the result.
Run a small complex example, explain the result, then change one input and compare the output.
FAQ
Does complex use general implicit expansion?⌄
No. A scalar component can expand, but two non-scalar components must have identical sizes.
Can binary complex accept complex components?⌄
No. Both components in complex(A, B) must be real numeric values.
What does unary complex do?⌄
It adds explicit zero imaginary storage to real input and preserves input that is already complex.
Why does isreal(complex(5)) return false?⌄
The value has complex storage with an explicit zero imaginary component, and isreal reports the storage domain.
Does complex accept logical values?⌄
Yes. Logical input produces complex double output.
How are integer components represented?⌄
RunMat retains their fixed-width class in exact paired real and imaginary storage.
Can the result remain on a provider?⌄
Yes. Supported floating-point construction stays resident; exact fallback restores typed results to the selected owner when possible.
Related Math functions
Elementwise
abs · angle · besselj · bsxfun · 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 complex is executed, line by line, in Rust.
- View the source for complex 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.