Matrix in Matlab can be created and manipulated
Matrix (a two-dimensional, rectangular shape used to store multiple elements of data in an easily accessible format) is the most basic data structure in Matlab. The elements of a 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 a matrix-based computing environment in which all of the data entered into Matlab is stored as a matrix.
The MATLAB environment uses the term matrix for a variable that contains real or complex numbers. These numbers are arranged in a two-dimensional grid. An array is, more generally, a vector, matrix, or higher dimensional grid of numbers. All variables in Matlab are multidimensional arrays, no matter what type of data they store. A matrix is a two-dimensional array often used for linear algebra.
It is assumed in this Matlab tutorial that you know some of the basics of how to define and manipulate vectors in Matlab software. we will discuss the following:
- Defining Matrix in Matlab
- Matrix Operations in Matlab
- Matrix Functions in Matlab
1) Define or Create a Matrix in Matlab
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 numbers are used to define the elements of the 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 vector entries can be referenced within parentheses. For example, A(2,3) represents an element in the second row and third column of matrix A.
A matrix in Matlab is a type of variable that is used for mathematical/statistical computation—some examples of creating a matrix in Matlab and extracting 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 a matrix
triu –> upper triangular part of a matrix
tril –> lower triangular part of a matrix
rand –> randomly generated matrix
hilb –> Hilbert matrix
magic –> magic square
2) Matrix Operations in Matlab
Many mathematical operations can be applied to 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$ be an invertible square matrix and $b$ be 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 in Matlab
Matlab has 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 decomposition
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
To learn more about the use of Matrices in Matlab, See the Matlab Help