Create a 2x3 matrix. Each row is a comma-separated list of elements. Rows are separated by a semicolon.

```
A = [1, 2, 3; 4, 5, 6]
# A =
#
# 1 2 3
# 4 5 6
```

Sum of two matrices

```
B = [1, 1, 1; 1, 1, 1]
# B =
#
# 1 1 1
# 1 1 1
A+B
# ans =
#
# 2 3 4
# 5 6 7
```

Multiply matrix by a scalar

```
2*A
# ans =
#
# 2 4 6
# 8 10 12
```

Matrix multiplication

```
C = [1, 0; 0, 0; 0, 1]
# C =
#
# 1 0
# 0 0
# 0 1
A*C
# ans =
#
# 1 3
# 4 6
```

A matrix can be a column vector

```
C = [2; 0; 1]
# C =
#
# 2
# 0
# 1
A * C
# ans =
#
# 5
# 14
```

Concatenating matrices

For horizontal concatenation, that is joining two block matrices column-wise

```
A= [1,2;3,4];
B=[4,3;2,1];
C=horzcat(A,B);
disp(C)
# C=
#
# 1 2 4 3
# 3 4 2 1
```

This modified text is an extract of the original Stack Overflow Documentation created by following contributors and released under CC BY-SA 3.0

This website is not affiliated with Stack Overflow