# Matlab for Matrices and Vectors

In my previous article I have discussed that matrices and vectors both are two branch of arrays. These are fundamental data types in matlab.

##### What is a matrix in matlab ?

Matrix can be defined as “It is a rectangular/square array in which numbers, symbols or expressions are represented in row and columns”.

- A matrix is a two dimensional array.
- Syntax to declare a matrix

*Note:*

- To create columns in matrix you need give space or comma between two elements as mentioned in above syntax
- To terminate a row you need to type a semicolon as mentioned in above syntax

Y = [a1 a2 a3; b1 b2 b3]

Y = [a1 a2 a3; b1 b2 b3] = a1 a2 a3 b1 b2 b3

##### Some shortcuts in matrix/slicing of a array

A single column can be selected as follows in matrix

col1 = Y(:,1)

In this case by using : you are allowing to access all rows of a array

col1 = Y(:,1) = a1 b1

A single row can be selected as follows in matrix

row1 = Y(1,:)

In this case by using : you are allowing to access all columns of a array

row1 = Y(1,:) = a1 a2 a3

A particular element of a matrix can be selected as follows

Particular_element = Y(2,3)

Particular_element = Y(2,3) = b3

To change a row vector into a column vector, you need to use ‘ as follows

x = (1:4) = 1 2 3 4 y = x' = (1:4)' = 1 2 3 4

##### What are vectors in matlab ?

Vector can also be defined as “In vectors numbers, symbols or expressions are represented in a single row and multi columns”.

- Vector is a one dimensional array.
- Syntax to declare a matrix

Y = [a1 a2 a3]

Y = [a1 a2 a3] = a1 a2 a3

##### Mathematical operations on Matrices and Vectors

To perform mathematical operations on matrices you need to have atleast two matrices. Suppose matrices are x and y

###### Multiplication

Multiplication of matrices are performed as follows:

- First, number of columns in first matrix (x) must be equal to the number of rows in second matrix (y).
- Matrix multiplication is not universally commutative for nonscalar inputs i.e, x*y is typically not equal to y*x.
- If at least one input is scalar, then x.*y is equivalent to y.*x and is commutative.
- To perform an element wise multiplication you need to use dot ( . ) operator before arthmetics operators.

**Multiplication of (row and column) vectors**

Suppose you have two vectors x and y

Row vector x = [1 2 3] or [1, 2, 3] Columns vector y = [2; 3; 4] Use syntax as follows to perfrom multiplication of vectros z = x*y % no need to dot operator = 2<strong>0</strong>

**Multiplication of arrays **

Suppose you have two arrays x and y as follows

x = [1 2 3; 4 5 2] y = [3 2 1; 2 5 4; 2 3 1] %% As we know number of column in x must be equal to %% number of rows in y z = x*y = 13 21 12 26 39 26

#### Element wise multiplication

**Multiplication of a matrix and a scalar quantity **

Suppose you have a array x and scalar k

z = k*[a1 a2 a3; b1 b2 b3] An errore is expected ??? To remove this error you have to use this expression as follows z = k.*[a1 a2 a2;b1 b2 b3] z = k*a1 k*a2 k*a3 k*b1 k*b2 k*b3

**Element wise multiplication of two vectors **

Again we need to assume two vectors x and y

x = [1 2 3] % row vector y = [3 2 1] % row vector z = x.*y = 3 4 3

** Element wise multiplication of two arrays **

Suppose you have two arrays x and y

x = [1 2 3; 3 4 5; 4 3 1] y = [3 2 1; 5 4 3; 1 3 4] z = x.*y = 3 4 3 15 16 15 4 9 4

**Element wise multiplication of a row and column vector **

Suppose you have a row and a column vector

x = [1 2 3] y = (1:5)' %% ' is used to change a row vector into column vector z = x.*y = 1 2 3 2 4 6 3 6 9 4 8 12 5 10 15