Main Content

equals, =

Variable creation and indexed assignment

Description

B = A stores the value of A in the variable B.

The = character assigns a value to a variable, whereas the == character compares the elements in two arrays.

example

B(i,j,...) = A assigns the elements of A to the indexed elements of B specified by the indexing expression B(i,j,...).

example

Examples

expand all

Use the = sign to assign a value to a variable.

A = 2
A = 
2

Because A has a single element, it is a scalar. Assign multiple values to a variable to create a vector or matrix.

B = [2 4 6 8]
B = 1×4

     2     4     6     8

C = [1 0; 0 1]
C = 2×2

     1     0
     0     1

You also can assign the contents of one variable to another. For example, overwrite the scalar stored in A with the vector in B.

A = B
A = 1×4

     2     4     6     8

Create a 3-by-3 matrix, and then index the first row to assign new elements.

A = [1 2 3; 4 5 6; 7 8 9]
A = 3×3

     1     2     3
     4     5     6
     7     8     9

A(1,:) = [10 10 10]
A = 3×3

    10    10    10
     4     5     6
     7     8     9

With indexed assignment, the number of elements indexed on the left side must equal the number of new elements on the right side.

Version History

Introduced before R2006a

See Also

|