# MATLAB Programming/Vector and Matrices

## What is scalar,vector and matrix ?

#### Scalar

Scalars are the physical quantities that are described by magnitude only. In other words, scalars are those quantities that are represented just by their numerical value, such as 3, -5, 0.368, etc.

#### Vector

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

This example vector A below is row vector

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

This example vector B 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. They are stored as two-dimensional arrays in MATLAB.
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 (i) AND one or more columns (j).

Back in Introduction chapter , MATLAB was originally designed to carry out operations with matrices, and received the name of MATrix LABoratory that subsequently was reduced to MATLAB.

It is possible to find applications of matrices not only in the mathematics areas, but also in a great deal of applications in engineering, in physics, in finance, in accounting, in chemistry and in biology, perhaps more.

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