# MATLAB Programming/Vector and Matrices

### What is scalar,vector and matrix ?

#### Scalar

A scalar consists of a single number, such as 3, -5, 0.368, etc.

#### Vector

A vector is a one dimensional array of numbers (can be in a row or column).

This example below is row vector

${\displaystyle \mathbf {A} ={\begin{bmatrix}5&8&6\\\end{bmatrix}}.}$

This example below is column vector

${\displaystyle \mathbf {B} ={\begin{bmatrix}7\\3\\2\end{bmatrix}}.}$

#### Matrix

A matrix is an ordered rectangular array arrangement of numbers.
A matrix having m rows and n columns is called a matrix of order m × n or simply m × n matrix.
It can be represented by one or more rows (m) AND one or more columns (n).

There are many types of matrices with followings as shown

 Rectangular Matrix (Where no. of row and column are unequal) Square Matrix (Where no. of row and column are same) Row Matrix (Matrix with one row , aka row vector) Column Matrix (Matrix with one column , aka column vector) Diagonal Matrix (Square Matrix with non-diagonal elements equal to zero ) ${\displaystyle \mathbf {\begin{bmatrix}9&5\\2&7\\8&3\end{bmatrix}} .}$ ${\displaystyle \mathbf {\begin{bmatrix}4&9&2\\3&5&7\\8&1&6\end{bmatrix}} }$ ${\displaystyle \mathbf {\begin{bmatrix}1&4&5\\\end{bmatrix}} }$ ${\displaystyle \mathbf {\begin{bmatrix}7\\9\\1\end{bmatrix}} }$ ${\displaystyle \mathbf {\begin{bmatrix}8&0&0\\0&7&0\\0&0&5\end{bmatrix}} }$

### Scalar in MATLAB

Scalar in MATLAB looks like assigning a variable to a number such as followed:

 a = 6


### Vectors in MATLAB

You can type following in MATLAB

For row vector , just type comma "," to separate each number

 >> VR = [6,2,5]
VR =
6     2     5


For column vector , just type semicolon ";" to separate each number

 >> VC = [9;1;6]
VC =
9
1
6


### Matrix in MATLAB

In MATLAB, to create matrix (or matrices), there is 3 important operator to be used

(a) Bracket "[" "]" as container for the matrix
(b) Comma , as matrix row separator
(c) Semicolon ; as matrix column separator

For example, we are creating 4X3 matrix in MATLAB using following commands.

>> M = [4,8,9,6;9,6,9,6;3,6,9,6]

M =
4     8     9     6
9     6     9     6
3     6     9     6