plus - Add numbers, append strings - MATLAB (original) (raw)

Add numbers, append strings

Syntax

Description

C = [A](#btx03j8-1-A) + [B](#btx03j8-1-A) adds arrays A and B by adding corresponding elements. If one input is a string array, then plus appends the corresponding elements as strings.

The sizes of A and B must be the same or becompatible. If the sizes ofA and B are compatible, then the two arrays implicitly expand to match each other. For example, if one ofA or B is a scalar, then the scalar is combined with each element of the other array. Also, vectors with different orientations (one row vector and one column vector) implicitly expand to form a matrix.

example

C = plus([A](#btx03j8-1-A),[B](#btx03j8-1-A)) is an alternate way to execute A + B, but is rarely used. It enables operator overloading for classes.

Examples

collapse all

Create an array, A, and add a scalar value to it.

A = [0 1; 1 0]; C = A + 2

The scalar value is added to each entry of A.

Create two 1-by-3 string arrays, then append similarly located strings in the arrays.

s1 = ["Red" "Blue" "Green"]

s1 = 1×3 string "Red" "Blue" "Green"

s2 = ["Truck" "Sky" "Tree"]

s2 = 1×3 string "Truck" "Sky" "Tree"

s = 1×3 string "RedTruck" "BlueSky" "GreenTree"

Create two arrays, A and B, and add them together.

A = [1 0; 2 4]; B = [5 9; 2 1]; C = A + B

The elements of A are added to the corresponding elements of B.

Create a 1-by-2 row vector and 3-by-1 column vector and add them.

a = 1:2; b = (1:3)'; a + b

The result is a 3-by-2 matrix, where each (i,j) element in the matrix is equal to a(j) + b(i):

a=[a1 a2], b=[b1b2b3], a+ b=[a1+ b1a2+ b1a1+ b2a2+ b2a1+ b3a2+ b3].

Create an array, A, and add a column vector to it. The vector is treated as though it is a matrix of the same size as A, so that each element in the vector is added to a row in A.

ans = 2×3

11    12    13

104 105 106

Since R2023a

Create two tables and add them. The row names (if present in both) and variable names must be the same, but do not need to be in the same orders. Rows and variables of the output are in the same orders as the first input.

A = table([1;2],[3;4],VariableNames=["V1","V2"],RowNames=["R1","R2"])

A=2×2 table V1 V2 __ __

R1    1     3 
R2    2     4 

B = table([4;2],[3;1],VariableNames=["V2","V1"],RowNames=["R2","R1"])

B=2×2 table V2 V1 __ __

R2    4     3 
R1    2     1 

C=2×2 table V1 V2 __ __

R1    2     5 
R2    5     8 

Input Arguments

collapse all

Operands, specified as scalars, vectors, matrices, multidimensional arrays, tables, or timetables. Inputs A andB must either be the same size or have sizes that are compatible (for example, A is anM-by-N matrix andB is a scalar or1-by-N row vector). For more information, see Compatible Array Sizes for Basic Operations.

Inputs that are tables or timetables must meet the following conditions: (since R2023a)

Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64 | logical | char | string | datetime | duration | calendarDuration | table | timetable
Complex Number Support: Yes

Tips

Extended Capabilities

expand all

Theplus function fully supports tall arrays. For more information, see Tall Arrays.

Inputs cannot be data type logical.

The plus function fully supports GPU arrays. To run the function on a GPU, specify the input data as a gpuArray (Parallel Computing Toolbox). For more information, see Run MATLAB Functions on a GPU (Parallel Computing Toolbox).

Version History

Introduced before R2006a

expand all

The plus operator supports operations directly on tables and timetables without indexing to access their variables. All variables must have data types that support the operation. For more information, see Direct Calculations on Tables and Timetables.

Starting in R2020b, plus supports implicit expansion when the arguments are calendarDuration, datetime, orduration arrays. Between R2020a and R2016b, implicit expansion was supported only for numeric and string data types.

Starting in R2016b with the addition of implicit expansion, some combinations of arguments for basic operations that previously returned errors now produce results. For example, you previously could not add a row and a column vector, but those operands are now valid for addition. In other words, an expression like [1 2] + [1; 2] previously returned a size mismatch error, but now it executes.

If your code uses element-wise operators and relies on the errors that MATLAB previously returned for mismatched sizes, particularly within a try/catch block, then your code might no longer catch those errors.

For more information on the required input sizes for basic array operations, see Compatible Array Sizes for Basic Operations.