Matrix (a two dimensional, rectangular shaped used to store multiple elements of data in an easy accessible format) is the most basic data structure in Matlab. The elements of matrix can be numbers, characters, logical states of yes or no (true or false) or other Matlab structure types. Matlab also supports more than two dimensional data structures, referred to as arrays in Matlab. Matlab is matrix-based computing environment in which all of the data entered into Matlab is stored as as a matrix.

It is assumed in this Matlab tutorial that you know some of the basics on how to define and manipulate vectors in Matlab software. we will discuss here

## 1) Defining/ Creating Matrices

Defining a matrix in Matlab is similar to defining a vector in Matlab. To define a matrix, treat it as a column of row vectors.

>> A=[1 2 3; 4 5 6; 7 8 9]

Note that spaces between number is used to define the elements of matrix and semi-colon is used to separate the rows of matrix A. The square brackets are used to construct matrices. The individual matrix and vectors entries can be referenced within parenthesis. For example A(2,3) represents element in second row and third column of matrix *A*.

Some example to create matrix and extract elements

>> A=rand(6, 6)

>> B=rand(6, 4)

>>A(1:4, 3) is a column vector consisting of the first four entries of the third column of *A*

>>A(:, 3) is the third column of *A*

>>A(1:4, : ) contains column and column 4 of matrix *A*

**Convenient matrix building Functions**

eye –> identity

zeros –> matrix of zeros

ones –> matrix of ones

diag –> create or extract diagonal elements of matrix

triu –> upper triangular part of matrix

tril –> lower triangular part of matrix

rand –> randomly generated matrix

hilb –> Hilbert matrix

magic –> magic square

## 2) Matrix Operations

Many of the mathematical operations can be applied on matrices and vectors in Matlab such as addition, subtraction, multiplication and division of matrices etc.

**Matrix or Vector Multiplication**

If x and y are both column vectors, then x’*y is their inner (or dot) product and x*y’ is their outer (or cross) product.

**Matrix division**

Let A is an invertible square matrix and b is a compatible column vector then

x = A/b is solution of A * x = b

x = b/A is solution of x * A = b

These are also called the backslash (\) and slash operators (/) also referred to as the mldivide and mrdivide.

## 3) Matrix Functions

Matlab has a many functions used to create different kinds of matrices. Some important matrix functions used in Matlab are

eig –> eigenvalues and eigenvectors

eigs –> like eig, for large sparse matrices

chol –> cholesky factorization

svd –> singular value decomposition

svds –> like svd, for large sparse matrices

inv –> inverse of matrix

lu –> LU factorization

qr –> QR factorization

hess –> Hessenberg form

schur –> Schur decompostion

rref –> reduced row echelon form

expm –> matrix exponential

sqrtm –> matrix square root

poly –> characteristic polynomial

det –> determinant of matrix

size –> size of an array

length –> length of a vector

rank –> rank of matrix