plus — Add arrays or append strings element by element.
plus(A, B) and A + B add compatible numeric, logical, character, sparse, symbolic, or provider-resident operands using implicit expansion. A string operand selects element-wise text addition with converted host operands.
Syntax
C = plus(A, B)
C = plus(A, B, "like", prototype)
S = plus(A, B)Inputs
A- Type
Any- Required
- Yes
- Description
- First numeric, logical, character, symbolic, sparse, or provider-resident operand.
B- Type
Any- Required
- Yes
- Description
- Second operand with a size compatible for implicit expansion.
like- Type
StringScalar- Required
- Yes
- Description
- Literal `like` selector for the RunMat output-prototype extension.
prototype- Type
LikePrototype- Required
- Yes
- Description
- Host, device, real, or complex output prototype.
A- Type
Any- Required
- Yes
- Description
- Host string or convertible numeric, logical, character, or scalar-convertible cell data; at least one operand must be a string.
B- Type
Any- Required
- Yes
- Description
- Second host operand; converted string shapes must permit implicit expansion.
Returns
C- Type
NumericArray- Description
- Element-wise result with the broadcasted operand shape.
S- Type
StringScalar- Description
- String scalar or array with the broadcasted converted shape; missing values propagate.
Errors
RunMat:plus:TooManyOutputs- When
- More than one output is requested.
- Message
plus: too many output arguments
RunMat:plus:InvalidArgument- When
- Optional arguments are malformed or unsupported.
- Message
plus: invalid argument
RunMat:plus:InvalidInput- When
- Operands or prototypes cannot be converted into the selected numeric, symbolic, or host string form.
- Message
plus: invalid input
Element-wise addition and implicit expansion
plus(A, B) and A + B add corresponding elements. Equal dimensions align directly and singleton dimensions expand to the other operand. Incompatible non-singleton dimensions produce a size-mismatch error. Empty dimensions remain empty in the broadcasted result.
Real and complex numeric operands may be combined. When neither operand is a string, character inputs contribute their Unicode code points and logical inputs contribute zero or one.
String addition
When either operand is a string scalar or string array, A + B appends text element by element. Numeric and logical operands convert to decimal text and true or false; character-matrix rows convert to string-column entries without trimming spaces. Cells must contain values that each convert to a scalar string. Converted shapes follow the same singleton-expansion rules as numeric addition.
Integer formatting reads the native integer payload, including values above the exact double range. Floating-point presentation uses RunMat's string-conversion spelling. Missing strings and NaN observations produce missing string elements. Unicode characters retain their UTF-16 row boundaries and UTF-8 output text.
String addition executes on the host and does not select a numeric GPU or fusion kernel. Gather provider-resident numeric values explicitly before appending them to text. The numeric like output-prototype form does not apply to string addition.
Numeric classes and sparse storage
Double inputs produce double. An operation involving single floating-point data produces single. Fixed-width integer addition accepts matching integer classes or one integer operand with scalar double, preserves the integer class, rounds fractional results according to the integer arithmetic contract, and saturates at the class bounds.
Sparse-sparse addition retains sparse storage when the expanded result remains sparse. Operations whose implicit zeros become nonzero use full storage, subject to the runtime materialization limit. Complex and mixed sparse forms follow the supported sparse arithmetic paths or return a typed error.
Accelerated execution and output prototypes
Compatible provider-resident operands use element-wise addition or scalar-add provider operations when available. Unsupported provider forms gather through the owning provider and use the same host arithmetic rules; provider failures are not reported as unsupported fallbacks.
RunMat mode accepts plus(A, B, 'like', prototype). A provider-resident prototype requests provider residency, a host prototype requests host residency, and a complex prototype promotes a real result to complex storage. This extension is rejected in MATLAB compatibility mode.
Examples
Append numbers to string prefixes
names = "sample-" + [1, 2]Expected output:
names = ["sample-1", "sample-2"]Add two matrices
A = [1 2 3; 4 5 6];
B = [7 8 9; 1 2 3];
S = plus(A, B)Expected output:
S = [8 10 12; 5 7 9]Add a scalar to an array
A = [8 1 6; 3 5 7; 4 9 2];
shifted = plus(A, 0.5)Expected output:
shifted = [8.5 1.5 6.5; 3.5 5.5 7.5; 4.5 9.5 2.5]Expand a column and row
col = (1:3)';
row = [10 20 30];
S = plus(col, row)Expected output:
S = [11 21 31; 12 22 32; 13 23 33]Add complex values
z1 = [1+2i, 3-4i];
z2 = [2-1i, -1+1i];
S = plus(z1, z2)Expected output:
S = [3+1i 2-3i]Add character code points
codes = plus('ABC', 2)Expected output:
codes = [67 68 69]Preserve a fixed-width integer class
S = plus(uint8([250 255]), uint8([10 1]))Expected output:
S = uint8([255 255])Request a provider-resident result
G1 = gpuArray([1 2 3]);
G2 = gpuArray([4 5 6]);
prototype = gpuArray(0);
deviceSum = plus(G1, G2, 'like', prototype);
S = gather(deviceSum)Expected output:
S = [5 7 9]Using plus with coding agents
Open a RunMat example with live inputs, then ask the agent to explain how plus changes the result.
Run a small plus example, explain the result, then change one input and compare the output.
FAQ
Does plus use implicit expansion?⌄
Yes. Singleton dimensions expand to compatible dimensions of the other operand; incompatible non-singleton dimensions produce a size-mismatch error.
What class does plus return?⌄
A string operand selects string output. Otherwise, floating arithmetic follows the participating floating class rules, integer arithmetic preserves the integer class when the operands use the same class or pair an integer with scalar double, and logical and character inputs produce numeric output.
How does integer overflow behave?⌄
Fixed-width integer sums saturate at the minimum or maximum of their class.
Can host scalars be added to provider-resident arrays?⌄
Yes. RunMat uses a provider scalar operation when supported and otherwise follows the owner-aware fallback path.
How can the result be placed on a particular device?⌄
In RunMat mode, pass 'like' and a provider-resident prototype. A host prototype requests a host result.
Does plus preserve sparse matrices?⌄
It preserves sparse storage when the mathematical result remains sparse. Forms that turn implicit zeros into nonzero values require materialization.
What happens with empty arrays?⌄
Compatible empty dimensions propagate into the broadcasted output shape.
Can plus append strings?⌄
Yes. A string operand selects text addition, converts supported host values to string elements, and implicitly expands their shapes. Character-plus-character without a string operand remains numeric arithmetic.
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 · 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 plus is executed, line by line, in Rust.
- View the source for plus 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.