## Assigning to Variables and Matrices

Be sure to read the indexing page first.

Assigning a matrix to a variable can be done using the = operator.

```>> A = [1 2 3; 3 5 2; 3 2 5]

A =

1     2     3
3     5     2
3     2     5

```

To change one entry in a matrix, assign to the indexed value:

```>> A(1,3) = 3.225

A =

1.0000    2.0000    3.2250
3.0000    5.0000    2.0000
3.0000    2.0000    5.0000

```

If a matrix is not assigned, a sufficiently large matrix is generated.

```>> B(3,5) = 2

B =

0     0     0     0     0
0     0     0     0     0
0     0     0     0     2

```

If a matrix is assigned, but does not contain the element, the matrix is made sufficiently large.

```>> B(5,5) = 3

B =

0     0     0     0     0
0     0     0     0     0
0     0     0     0     2
0     0     0     0     0
0     0     0     0     3

```

You can assign to a block within a matrix. If the right hand side is a scalar, all values within that block are assigned that value. If the right hand side is a matrix, it must have the same dimensions of the block you are assigning to. The matrix is automatically enlarged if you index outside the matrix.

```>> A = [1:5; 2:6; 3:7]

A =

1     2     3     4     5
2     3     4     5     6
3     4     5     6     7

>> A(1:4, 5) = 1

A =

1     2     3     4     1
2     3     4     5     1
3     4     5     6     1
0     0     0     0     1

>> A([1 3], [1 3:4]) = [-1 -2 -3; -4 -5 -6]

-1     2    -2    -3     1
2     3     4     5     1
-4     4    -5    -6     1
0     0     0     0     1

```

The colon and end can also be used to denote a row:

```>> A(:, end-1) = 2

-1     2    -2     2     1
2     3     4     2     1
-4     4    -5     2     1
0     0     0     2     1

```