typecast — Reinterpret numeric bytes as another class without converting values.
typecast preserves the native byte sequence of a full numeric or logical scalar or vector and decodes that sequence with a selected output representation.
Syntax
Y = typecast(X, newtype)
Y = typecast(X, "like", prototype)Inputs
X- Type
NumericArray- Required
- Yes
- Description
- Full numeric or logical scalar or vector.
newtype- Type
StringScalar- Required
- Yes
- Description
- Numeric or logical output class.
like- Type
StringScalar- Required
- Yes
- Description
- Literal string "like".
prototype- Type
LikePrototype- Required
- Yes
- Description
- Host output class and complexity prototype.
Returns
Y- Type
NumericArray- Description
- Reinterpreted scalar or vector.
Errors
RunMat:typecast:InvalidArgument- When
- The requested class, prototype, or argument list is invalid.
- Message
typecast: invalid argument
RunMat:typecast:InvalidInput- When
- The input is sparse, is not a scalar or vector, or has an incompatible byte count.
- Message
typecast: invalid input
RunMat:typecast:GpuUnsupported- When
- A GPU call uses complex or logical input or the like syntax.
- Message
typecast: unsupported gpuArray form
RunMat:typecast:Internal- When
- Exact gather, reconstruction, or resident restoration fails.
- Message
typecast: internal error
Byte reinterpretation
Y = typecast(X, newtype) reads the native byte sequence of a full numeric or logical scalar or vector as elements of newtype. It does not numerically convert the values.
The source byte count must be divisible by the output element width. A row remains a row and a column remains a column; changing the element width changes only the number of elements.
Classes and native byte order
Double, single, logical, and all eight fixed-width integer classes use their authoritative storage. Multi-byte values follow the platform's native byte order, so byte-vector presentation can differ between little-endian and big-endian systems while a same-platform round trip remains exact.
Character reinterpretation is not available because RunMat's character representation cannot preserve every raw UTF-16 code unit. Sparse arrays and nonscalar matrices are rejected.
Prototype selection
Y = typecast(X, "like", prototype) selects the output class and real or complex storage from a host prototype. A complex prototype pairs adjacent decoded components as real and imaginary values.
The prototype controls representation only; its value and shape do not contribute to the output.
Accelerated execution
Real numeric gpuArray input is gathered exactly through its owning provider, reinterpreted on the host, and restored to the same owner. RunMat preserves fixed-width integer bytes without a floating-point intermediary.
Complex and logical gpuArray input and the like form with resident values are rejected before transfer. typecast is a fusion boundary.
Examples
Inspect the native bytes of unsigned integers
values = uint32([1 256]);
bytes = typecast(values, "uint8")Expected output:
bytes is a 1-by-8 uint8 vector in native byte orderRound-trip wide integer bit patterns exactly
values = uint64([9007199254740993 intmax("uint64")]);
copy = typecast(typecast(values, "uint8"), "uint64")Expected output:
copy retains both uint64 values exactlySelect paired complex integer output with a prototype
z = typecast(int16([-1 2 -3 4]), "like", complex(int16(0)))Expected output:
z = [-1+2i -3+4i] with complex int16 storagePreserve column orientation while changing width
words = uint32([1; 2; 3]);
bytes = typecast(words, "uint8")Expected output:
bytes is a 12-by-1 uint8 columnUsing typecast with coding agents
Open a RunMat example with live inputs, then ask the agent to explain how typecast changes the result.
Run a small typecast example, explain the result, then change one input and compare the output.
FAQ
Does typecast convert numeric values?⌄
No. It preserves the source bytes and changes how fixed-width elements are interpreted.
Why can the number of elements change?⌄
The byte count stays fixed. Reading the same bytes with a narrower class produces more elements, and a wider class produces fewer.
Which byte order does typecast use?⌄
It uses the host platform's native byte order, matching the native in-memory representation.
What does the like form copy?⌄
It copies the prototype's class and real-or-complex representation, not its data or shape.
Can typecast accept matrices?⌄
Only scalar and vector input is supported. A nonscalar matrix is rejected.
Can typecast remain resident on a GPU provider?⌄
Supported real numeric input returns to its original owner after exact host reinterpretation; unsupported resident forms fail explicitly.
Related Math functions
Elementwise
abs · 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 · 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 typecast is executed, line by line, in Rust.
- View the source for typecast 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.