max(x)
: Finds the largest value in a vector x
x = [2 7 3]
max(x)
Creates a row vector containing the maximum element from each column of a matrix x.
x = [2 7 3; 1 8 6]
max(x)
[a,b]=max(x)
: Finds both the largest value in vector x (a) and its location in vector x (b)
x=[2 7 3]
[a,b]=max(x)
Creates a row vector containing the maximum element from each column of a matrix x abd a row vector with the location of the max in each column of matrix x.
x=[2 7 3; 1 8 6]
[a,b]=max(x)
max(x,y)
: Creates a matrix the same size as x and y. They both must be the same size. Each element in the resulting matrix contains the max value from the corresponding positions in x and y.
x=[2 7 3; 1 8 6]
y=[9 2 4; 3 5 7]
max(x,y)
min(x)
: Finds the smallest value in a vector x
x=[2 7 3]
min(x)
Creates a row vector containing the minimum element from each column of a matrix x.
x=[2 7 3; 1 8 6]
min(x)
[a,b]=min(x)
: Finds both the smallestvalue in vector x (a) and its location in vector x (b)
x=[2 7 3]
[a,b]=min(x)
Creates a row vector containing the minimum element from each column of a matrix x abd a row vector with the location of the min in each column of matrix x.
x=[2 7 3; 1 8 6]
[a,b]=min(x)
min(x,y)
: Creates a matrix the same size as x and y. They both must be the same size. Each element in the resulting matrix contains the min value from the corresponding positions in x and y.
x=[2 7 3; 1 8 6]
y=[9 2 4; 3 5 7]
min(x,y)
mean(x)
: Computes the mean value (average value) of a vector x.
x=[2 7 3]
mean(x)
Returns a row vector containing the mean from each column of a matrix x.
x=[2 7 3; 1 8 6]
mean(x)
median(x)
: Computes the median value of a vector x.
x=[2 7 3]
median(x)
Returns a row vector containing the median from each column of a matrix x.
x=[2 7 3; 1 8 6; 3 5 4]
median(x)
mode(x)
: Finds the value that occurs most often in an array
x=[2 7 3 3]
mode(x)
sum(x)
: Sums the elements in vector x.
x=[2 7 3]
sum(x)
Computes a row vector containing the sum of the elements in each column of a matrix x.
x=[2 7 3; 1 8 6]
sum(x)
prod(x)
: Computes the products of the elements of a vector x.
x=[2 7 3]
prod(x)
Computes a row vector containing the product of the elements in each column of a matrix x.
x=[2 7 3; 1 8 6]
prod(x)
cumsum(x)
: Computes a vector containing cumulative sums of the elements of a vector x.
x=[2 7 3]
cumsum(x)
Computes a matrix containing the cumulative sum of the elements in each column of a matrix x.
x=[2 7 3; 1 8 6]
cumsum(x)
cumprod(x)
: Computes a vector containing cumulative products of the elements of a vector x.
x=[2 7 3]
cumprod(x)
Computes a matrix containing the cumulative product of the elements in each column of a matrix x.
x=[2 7 3; 1 8 6]
cumprod(x)
sort(x)
: Sorts the elements of a vector x in ascending order.
x=[2 7 3]
sort(x)
Sorts the elements in each column of a matrix x in ascending order.
x=[2 7 3; 1 8 6]
sort(x)
sort(x,'descend')
: Sorts the elements in each column of a matrix x in descending order.
x=[2 7 3; 1 8 6]
sort(x,'descend')
sortrows(x)
: Sorts the rows of a matrix in ascending order on the basis of the values in the first column and keeps each row intact.
x=[2 7 3; 1 8 6; 9 2 5]
sortrows(x)
sortrows(x,n)
: Sorts the rows of a matrix in ascending order on the basis of the values in column n and keeps each row intact.
x=[2 7 3; 1 8 6; 9 2 5]
sortrows(x,2)
size(x)
: Determines the number of rows and columns in matrix x. If x is a multidimensional array, size determines how many dimensions exist and how big they are.
x=[2 7 3; 1 8 6]
size(x)
[a,b]=size(x)
: Determines the number of rows (a) and the number of columns (b) in a matrix x.
x=[2 7 3; 1 8 6]
[a,b]=size(x)
length(x)
: Determines the largest dimension of a matrix x.
x=[2 7 3; 1 8 6]
length(x)
numel(x)
: Determines the total number of elements in a matrix x.
x=[2 7 3; 1 8 6]
numel(x)
std(x)
: Computes the standard deviation of the values in a vector x.
x=[2 7 3]
std(x)
Returns a row vector containing a standard deviation calculated for each column of a matrix x.
x=[2 7 3; 1 8 6]
std(x)
var(x)
: Calculates the variance of the data in matrix x
x=[2 7 3]
var(x)
corrcoef(A, B)
: Returns correlation coefficients between two random variables A and B.
A=randn(10,1)
B=randn(10,1)
corrcoef(A,B)
rand(n)
: Returns an $n\times n$ matrix. Each value in the matrix is a random number between 0 and 1.
rand(3)
rand(m,n)
: Returns an $m\times n$ matrix. Each value in the matrix is a random number between 0 and 1.
rand(3,5)
randn(n)
: Returns an $n\times n$ matrix. Each value in the matrix is a Gaussian (or normal) random number with a mean of 0 and a variance 1.
randn(3)
randn(m,n)
: Returns an $m \times n$ matrix. Each value in the matrix is a Gaussian (or normal) random number with a mean of 0 and a variance 1.
randn(3,5)
abs(x)
: Computes the absolute value of a complex number, using the Pythagoron theorem. This is equivalent to the radius if the complex number is represented in polar coordinates.
x=5+12i
abs(x)
angle(x)
: Computes the angle from the horizontal in radians when a complex number is represented in polar coordinates.
x=5+12i
angle(x)
complex(x,y)
: Generates a complex number with a real component x and an imaginary component y.
x=5
y=12
complex(x,y)
real(x)
: Extracts the real component from a complex number.
x=5+12i
real(x)
imag(x)
: Extracts the imaginary component from a complex number.
x=5+12i
imag(x)
isreal(x)
: Determines whether the values in an array are real. If they are, the function returns 1; if they are complex, it returns 0.
x=5+12i
isreal(x)
conj(x)
: Generates the complex conjugate of a complex number.
x=5+12i
conj(x)
pi
: Mathematical constant $\pi$.
pi
i
: Imaginary number.
i
j
: Imaginary number.
j
inf
: Infinity, which often occurs during a calculational overflow or when a number is divided by 0.
9/0
NaN
: Not a number. Occurs when a calculation is undefined.
0/0
Matrices can be introduced into MATLAB in several different ways:
X = [-2 6 4 1] % Row vector
Y = [-2,6,4,1] % Row vector
Z = [-2;6;4;1] % Column vector
W = [3 -1 10 -8; 9 4 -5 7; 6 2 -7 0] % the dimensions are 4x3
A = [-2 6]
B = [4, A]
B(4) = 3.7
B(5) = -0.8
C = [3 -1 10 2 0; B]
eye(n)
: creates an $n\times n$ identity matrix.
eye(4)
zeros(m,n)
: creates an $m\times n$ matrix of zeros.
zeros(3,4)
ones(m,n)
: creates an $m\times n$ matrix of ones.
ones(4,3)
rand(n)/randn(n)
: creates an $n\times n$ random matrix.
rand(3)
randn(3)
rand(m,n)/randn(m,n)
: creates an $m\times n$ random matrix.
rand(3,4)
randn(3,4)
Let $A$ be $$A = \begin{bmatrix} 3 & 10 & -1 \\ 9 & 4 & 5 \\ 6 & 2 & -7 \end{bmatrix}$$
A = [3 10 -1; 9 4 5; 6 2 -7]
triu(A)
: Returns a matrix including upper triangular of matrix A.
triu(A)
tril(A)
: Returns a matrix including lower triangular of matrix A.
tril(A)
A(i,j)
: Returns the element in row i and column j of matrix A.
A(2,3)
A(3,1)
A(1,1) + A(1,2) + A(1,3) %The sum of row 1
Let $J$ be $$J=\begin{bmatrix} 3 & 1 & 4 & 6 & 2 \\ 0 & 1 & 6 & 8 & 3 \\ 7 & 5 & 9 & 0 & 4 \end{bmatrix}$$
J=[3 1 4 6 2; 0 1 6 8 3; 7 5 9 0 4]
J(:,1) % returns a column vector containing al rows in column 1
J(3,:) % returns a row vector containing all rows in column 1
J(2:3,:) % rows 2 to 3 all columns
J(2:3,4:5) % rows 2 to 3 in columns 4 to 5
J(:,[1 3]) % rows in columns 1 and 3
J(:) % transforms the matrix into one long column
J(2,end)
J(end,4)
J(end)
A'
: Calculates the transpose of matrix A.
A
A'
When used with complex numbers, the transpose operation returns the complex conjugate.
K=[1-i 6+3i; 8-5i 4+2i]
K'
dot(X,Y)
: Calculates the dot product of two vectors.
X=[-2 6 9];
Y=[3 -1 4];
dot(X,Y)
dot(Y,X)
cross(X,Y)
: Calculates the cross product of two vectors.
X=[-2 6 9];
Y=[3 -1 4];
cross(X,Y)
cross(Y,X)
det(A)
: Calculates the determinant of matrix A.
det(A)
repmat(A,m,n)
: Creates a large matrix consisting of an m-by-n tiling of copies of A.
repmat(A,1,2)
diag(A)
: Extracts the diagonal of matrix A.
diag(A)
diag(A,1)
diag(A,2)
diag(A,-1)
diag(A,-2)
For any vector D, diag(D)
creates a square matrix with D as the diagonal.
D=[3 10 5];
diag(D)
diag(D,1)
diag(D,-1)
blkdiag
: Generates block diagonal concatenation of matrix input arguments.
E=[3 5]; F=[1 2; 3 4]; G=[8];
blkdiag(E,F,G)
blkdiag(G,F)
fliplr(H)
: flips the matrix H into its mirror image, from right to left.
H=[1 0 0; 0 2 0; 0 0 3]
fliplr(H)
flipud(H)
: flips the matrix H vectically.
flipud(H)
Magic Matrix: A square matrix where the numbers in each row, and in each column, and the numbers in the main and secondary diagonals, all add up to the same number.
magic(m)
: Creates and $m\times m$ magic matrix.
magic(3)
magic(4)
inv(A)
: Calculates the inverse of matrix A.
inv(A)
rank(A)
: Calculates the rank of matrix A.
rank(A)
K=[1 2 1; -2 -3 1; 3 5 0]
rank(K)
Consider the following system of three equations with three unknowns: \begin{eqnarray} 3x + 2y + z &=& 10 \ -x + 3y + 2z &=& 5 \ x - y - z &=& -1 \end{eqnarray} We can rewrite this system of equations by using the following matrices:
$$\begin{bmatrix} 3 & 2 & 1 \\ -1 & 3 & 2 \\ 1 & -1 & -1 \end{bmatrix} \begin{bmatrix} x \\ y \\ z \end{bmatrix} = \begin{bmatrix} 10 \\ 5 \\ -1 \end{bmatrix}$$Using matrix multiplication, we can the write the system of equations
$$MX=N$$We can multiply both sides of the matrix equation above by $M^{-1}$(if exists) to get
$$M^{-1}MX=M^{-1}N$$giving
$$X=M^{-1}N$$So we can use the following command to solve this problem:
M=[3 2 1; -1 3 2; 1 -1 -1]
N=[10; 5 ; -1]
X = inv(M)*N
So the solution to the system is
$$X=\begin{bmatrix} 2 \\ 1 \\ 2 \end{bmatrix}$$