Documentation

This is machine translation

Translated by Microsoft
Mouse over text to see original. Click the button below to return to the English verison of the page.

plus, +

Syntax

Description

example

C = A + B adds arrays A and B and returns the result in C.

C = plus(A,B) 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
C =

     2     3
     3     2

The scalar value is added to each entry of A.

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

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

     6     9
     4     5

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

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.

A = [1 2 3; 4 5 6]
A =

     1     2     3
     4     5     6

b = [10; 100]
b =

    10
   100

A + b
ans =

    11    12    13
   104   105   106

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

s1 = string({'Red' 'Blue' 'Green'})
s1 = 

  1×3 string array

    "Red"    "Blue"    "Green"

s2 = string({'Truck' 'Sky' 'Tree'})
s2 = 

  1×3 string array

    "Truck"    "Sky"    "Tree"

s = s1 + s2
s = 

  1×3 string array

    "RedTruck"    "BlueSky"    "GreenTree"

Input Arguments

collapse all

Input array, specified as a scalar, vector, matrix, or multidimensional array. Numeric or string inputs A and B must either be the same size or have sizes that are compatible (for example, A is an M-by-N matrix and B is a scalar or 1-by-N row vector). For more information, see Compatible Array Sizes for Basic Operations.

Datetime, duration, and calendar duration arrays must be the same size unless one is a scalar.

  • If one input is a datetime array, duration array, or calendar duration array, then numeric values in the other input are treated as a number of 24-hour days.

  • If one input is a string array, then the other input can be a numeric, logical, character, string, or cell array.

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

Input array, specified as a scalar, vector, matrix, or multidimensional array. Numeric or string inputs A and B must either be the same size or have sizes that are compatible (for example, A is an M-by-N matrix and B is a scalar or 1-by-N row vector). For more information, see Compatible Array Sizes for Basic Operations.

Datetime, duration, and calendar duration arrays must be the same size unless one is a scalar.

  • If one input is a datetime array, duration array, or calendar duration array, then numeric values in the other input are treated as a number of 24-hour days.

  • If one input is a string array, then the other input can be a numeric, logical, character, string, or cell array.

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

More About

collapse all

Tall Array Support

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

See Also

| | |

Introduced before R2006a

Was this topic helpful?