Data Analysis Functions

max(x) : Finds the largest value in a vector x

In [11]:
x = [2 7 3]
max(x)
x =

     2     7     3


ans =

     7


Creates a row vector containing the maximum element from each column of a matrix x.

In [7]:
x = [2 7 3; 1 8 6]
max(x)
x =

     2     7     3
     1     8     6


ans =

     2     8     6


[a,b]=max(x) : Finds both the largest value in vector x (a) and its location in vector x (b)

In [12]:
x=[2 7 3]
[a,b]=max(x)
x =

     2     7     3


a =

     7


b =

     2


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.

In [9]:
x=[2 7 3; 1 8 6]
[a,b]=max(x)
x =

     2     7     3
     1     8     6


a =

     2     8     6


b =

     1     2     2


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.

In [10]:
x=[2 7 3; 1 8 6]
y=[9 2 4; 3 5 7]
max(x,y)
x =

     2     7     3
     1     8     6


y =

     9     2     4
     3     5     7


ans =

     9     7     4
     3     8     7


min(x) : Finds the smallest value in a vector x

In [13]:
x=[2 7 3]
min(x)
x =

     2     7     3


ans =

     2


Creates a row vector containing the minimum element from each column of a matrix x.

In [14]:
x=[2 7 3; 1 8 6]
min(x)
x =

     2     7     3
     1     8     6


ans =

     1     7     3


[a,b]=min(x) : Finds both the smallestvalue in vector x (a) and its location in vector x (b)

In [15]:
x=[2 7 3]
[a,b]=min(x)
x =

     2     7     3


a =

     2


b =

     1


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.

In [16]:
x=[2 7 3; 1 8 6]
[a,b]=min(x)
x =

     2     7     3
     1     8     6


a =

     1     7     3


b =

     2     1     1


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.

In [17]:
x=[2 7 3; 1 8 6]
y=[9 2 4; 3 5 7]
min(x,y)
x =

     2     7     3
     1     8     6


y =

     9     2     4
     3     5     7


ans =

     2     2     3
     1     5     6


mean(x) : Computes the mean value (average value) of a vector x.

In [18]:
x=[2 7 3]
mean(x)
x =

     2     7     3


ans =

     4


Returns a row vector containing the mean from each column of a matrix x.

In [19]:
x=[2 7 3; 1 8 6]
mean(x)
x =

     2     7     3
     1     8     6


ans =

    1.5000    7.5000    4.5000


median(x) : Computes the median value of a vector x.

In [20]:
x=[2 7 3]
median(x)
x =

     2     7     3


ans =

     3


Returns a row vector containing the median from each column of a matrix x.

In [21]:
x=[2 7 3; 1 8 6; 3 5 4]
median(x)
x =

     2     7     3
     1     8     6
     3     5     4


ans =

     2     7     4


mode(x) : Finds the value that occurs most often in an array

In [23]:
x=[2 7 3 3]
mode(x)
x =

     2     7     3     3


ans =

     3


sum(x) : Sums the elements in vector x.

In [24]:
x=[2 7 3]
sum(x)
x =

     2     7     3


ans =

    12


Computes a row vector containing the sum of the elements in each column of a matrix x.

In [25]:
x=[2 7 3; 1 8 6]
sum(x)
x =

     2     7     3
     1     8     6


ans =

     3    15     9


prod(x) : Computes the products of the elements of a vector x.

In [26]:
x=[2 7 3]
prod(x)
x =

     2     7     3


ans =

    42


Computes a row vector containing the product of the elements in each column of a matrix x.

In [27]:
x=[2 7 3; 1 8 6]
prod(x)
x =

     2     7     3
     1     8     6


ans =

     2    56    18


cumsum(x) : Computes a vector containing cumulative sums of the elements of a vector x.

In [28]:
x=[2 7 3]
cumsum(x)
x =

     2     7     3


ans =

     2     9    12


Computes a matrix containing the cumulative sum of the elements in each column of a matrix x.

In [29]:
x=[2 7 3; 1 8 6]
cumsum(x)
x =

     2     7     3
     1     8     6


ans =

     2     7     3
     3    15     9


cumprod(x) : Computes a vector containing cumulative products of the elements of a vector x.

In [30]:
x=[2 7 3]
cumprod(x)
x =

     2     7     3


ans =

     2    14    42


Computes a matrix containing the cumulative product of the elements in each column of a matrix x.

In [31]:
x=[2 7 3; 1 8 6]
cumprod(x)
x =

     2     7     3
     1     8     6


ans =

     2     7     3
     2    56    18


Sorting Functions

sort(x) : Sorts the elements of a vector x in ascending order.

In [32]:
x=[2 7 3]
sort(x)
x =

     2     7     3


ans =

     2     3     7


Sorts the elements in each column of a matrix x in ascending order.

In [33]:
x=[2 7 3; 1 8 6]
sort(x)
x =

     2     7     3
     1     8     6


ans =

     1     7     3
     2     8     6


sort(x,'descend') : Sorts the elements in each column of a matrix x in descending order.

In [34]:
x=[2 7 3; 1 8 6]
sort(x,'descend')
x =

     2     7     3
     1     8     6


ans =

     2     8     6
     1     7     3


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.

In [35]:
x=[2 7 3; 1 8 6; 9 2 5]
sortrows(x)
x =

     2     7     3
     1     8     6
     9     2     5


ans =

     1     8     6
     2     7     3
     9     2     5


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.

In [36]:
x=[2 7 3; 1 8 6; 9 2 5]
sortrows(x,2)
x =

     2     7     3
     1     8     6
     9     2     5


ans =

     9     2     5
     2     7     3
     1     8     6


Size Functions

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.

In [37]:
x=[2 7 3; 1 8 6]
size(x)
x =

     2     7     3
     1     8     6


ans =

     2     3


[a,b]=size(x) : Determines the number of rows (a) and the number of columns (b) in a matrix x.

In [38]:
x=[2 7 3; 1 8 6]
[a,b]=size(x)
x =

     2     7     3
     1     8     6


a =

     2


b =

     3


length(x) : Determines the largest dimension of a matrix x.

In [39]:
x=[2 7 3; 1 8 6]
length(x)
x =

     2     7     3
     1     8     6


ans =

     3


numel(x) : Determines the total number of elements in a matrix x.

In [40]:
x=[2 7 3; 1 8 6]
numel(x)
x =

     2     7     3
     1     8     6


ans =

     6


Statistical Functions

std(x) : Computes the standard deviation of the values in a vector x.

In [41]:
x=[2 7 3]
std(x)
x =

     2     7     3


ans =

    2.6458


Returns a row vector containing a standard deviation calculated for each column of a matrix x.

In [42]:
x=[2 7 3; 1 8 6]
std(x)
x =

     2     7     3
     1     8     6


ans =

    0.7071    0.7071    2.1213


var(x) : Calculates the variance of the data in matrix x

In [43]:
x=[2 7 3]
var(x)
x =

     2     7     3


ans =

     7


corrcoef(A, B) : Returns correlation coefficients between two random variables A and B.

In [45]:
A=randn(10,1)
B=randn(10,1)
corrcoef(A,B)
A =

    0.6715
   -1.2075
    0.7172
    1.6302
    0.4889
    1.0347
    0.7269
   -0.3034
    0.2939
   -0.7873


B =

    0.8884
   -1.1471
   -1.0689
   -0.8095
   -2.9443
    1.4384
    0.3252
   -0.7549
    1.3703
   -1.7115


ans =

    1.0000    0.3275
    0.3275    1.0000


Random Number Generators

rand(n) : Returns an $n\times n$ matrix. Each value in the matrix is a random number between 0 and 1.

In [46]:
rand(3)
ans =

    0.4387    0.7952    0.4456
    0.3816    0.1869    0.6463
    0.7655    0.4898    0.7094


rand(m,n) : Returns an $m\times n$ matrix. Each value in the matrix is a random number between 0 and 1.

In [49]:
rand(3,5)
ans =

    0.1966    0.4733    0.5853    0.2858    0.3804
    0.2511    0.3517    0.5497    0.7572    0.5678
    0.6160    0.8308    0.9172    0.7537    0.0759


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.

In [50]:
randn(3)
ans =

   -1.1480    2.5855   -0.0825
    0.1049   -0.6669   -1.9330
    0.7223    0.1873   -0.4390


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.

In [51]:
randn(3,5)
ans =

   -1.7947    0.1001   -0.6003    1.7119   -0.8396
    0.8404   -0.5445    0.4900   -0.1941    1.3546
   -0.8880    0.3035    0.7394   -2.1384   -1.0722


Functions Used with Complex Numbers

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.

In [52]:
x=5+12i
abs(x)
x =

   5.0000 +12.0000i


ans =

    13


angle(x) : Computes the angle from the horizontal in radians when a complex number is represented in polar coordinates.

In [53]:
x=5+12i
angle(x)
x =

   5.0000 +12.0000i


ans =

    1.1760


complex(x,y) : Generates a complex number with a real component x and an imaginary component y.

In [54]:
x=5
y=12
complex(x,y)
x =

     5


y =

    12


ans =

   5.0000 +12.0000i


real(x) : Extracts the real component from a complex number.

In [55]:
x=5+12i
real(x)
x =

   5.0000 +12.0000i


ans =

     5


imag(x) : Extracts the imaginary component from a complex number.

In [56]:
x=5+12i
imag(x)
x =

   5.0000 +12.0000i


ans =

    12


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.

In [57]:
x=5+12i
isreal(x)
x =

   5.0000 +12.0000i


ans =

  logical

   0


conj(x) : Generates the complex conjugate of a complex number.

In [58]:
x=5+12i
conj(x)
x =

   5.0000 +12.0000i


ans =

   5.0000 -12.0000i


Special Functions

pi : Mathematical constant $\pi$.

In [59]:
pi
ans =

    3.1416


i : Imaginary number.

In [60]:
i
ans =

   0.0000 + 1.0000i


j : Imaginary number.

In [61]:
j
ans =

   0.0000 + 1.0000i


inf : Infinity, which often occurs during a calculational overflow or when a number is divided by 0.

In [62]:
9/0
ans =

   Inf


NaN : Not a number. Occurs when a calculation is undefined.

In [63]:
0/0
ans =

   NaN


How to Introduce Matrices into MATLAB

Matrices can be introduced into MATLAB in several different ways:

  • Manual entry of a matrix
  • Generating a matrix using Operations and Functions
  • Loading a matrix (load filename)
  • To create a row, either comma or space are used
  • To create a column, semicolon is used
In [65]:
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
X =

    -2     6     4     1


Y =

    -2     6     4     1


Z =

    -2
     6
     4
     1


W =

     3    -1    10    -8
     9     4    -5     7
     6     2    -7     0


In [66]:
A = [-2 6]
A =

    -2     6


In [67]:
B = [4, A]
B =

     4    -2     6


In [69]:
B(4) = 3.7
B =

    4.0000   -2.0000    6.0000    3.7000


In [70]:
B(5) = -0.8
B =

    4.0000   -2.0000    6.0000    3.7000   -0.8000


In [72]:
C = [3 -1 10 2 0; B]
C =

    3.0000   -1.0000   10.0000    2.0000         0
    4.0000   -2.0000    6.0000    3.7000   -0.8000


Matrix Building Functions

eye(n) : creates an $n\times n$ identity matrix.

In [1]:
eye(4)
ans =

     1     0     0     0
     0     1     0     0
     0     0     1     0
     0     0     0     1


zeros(m,n) : creates an $m\times n$ matrix of zeros.

In [2]:
zeros(3,4)
ans =

     0     0     0     0
     0     0     0     0
     0     0     0     0


ones(m,n) : creates an $m\times n$ matrix of ones.

In [3]:
ones(4,3)
ans =

     1     1     1
     1     1     1
     1     1     1
     1     1     1


rand(n)/randn(n) : creates an $n\times n$ random matrix.

In [4]:
rand(3)
ans =

    0.8147    0.9134    0.2785
    0.9058    0.6324    0.5469
    0.1270    0.0975    0.9575


In [5]:
randn(3)
ans =

    2.7694    0.7254   -0.2050
   -1.3499   -0.0631   -0.1241
    3.0349    0.7147    1.4897


rand(m,n)/randn(m,n) : creates an $m\times n$ random matrix.

In [6]:
rand(3,4)
ans =

    0.7922    0.0357    0.6787    0.3922
    0.9595    0.8491    0.7577    0.6555
    0.6557    0.9340    0.7431    0.1712


In [7]:
randn(3,4)
ans =

    0.8884   -0.8095    0.3252   -1.7115
   -1.1471   -2.9443   -0.7549   -0.1022
   -1.0689    1.4384    1.3703   -0.2414


Let $A$ be $$A = \begin{bmatrix} 3 & 10 & -1 \\ 9 & 4 & 5 \\ 6 & 2 & -7 \end{bmatrix}$$

In [8]:
A = [3 10 -1; 9 4 5; 6 2 -7]
A =

     3    10    -1
     9     4     5
     6     2    -7


triu(A) : Returns a matrix including upper triangular of matrix A.

In [9]:
triu(A)
ans =

     3    10    -1
     0     4     5
     0     0    -7


tril(A) : Returns a matrix including lower triangular of matrix A.

In [10]:
tril(A)
ans =

     3     0     0
     9     4     0
     6     2    -7


Referencing Matrix Elements, Submatrices and Colon Notation

A(i,j) : Returns the element in row i and column j of matrix A.

In [11]:
A(2,3)
ans =

     5


In [12]:
A(3,1)
ans =

     6


In [14]:
A(1,1) + A(1,2) + A(1,3) %The sum of row 1
ans =

    12


Let $J$ be $$J=\begin{bmatrix} 3 & 1 & 4 & 6 & 2 \\ 0 & 1 & 6 & 8 & 3 \\ 7 & 5 & 9 & 0 & 4 \end{bmatrix}$$

In [15]:
J=[3 1 4 6 2; 0 1 6 8 3; 7 5 9 0 4]
J =

     3     1     4     6     2
     0     1     6     8     3
     7     5     9     0     4


In [16]:
J(:,1) % returns a column vector containing al rows in column 1
ans =

     3
     0
     7


In [17]:
J(3,:) % returns a row vector containing all rows in column 1
ans =

     7     5     9     0     4


In [18]:
J(2:3,:) % rows 2 to 3 all columns
ans =

     0     1     6     8     3
     7     5     9     0     4


In [19]:
J(2:3,4:5) % rows 2 to 3 in columns 4 to 5
ans =

     8     3
     0     4


In [20]:
J(:,[1 3]) % rows in columns 1 and 3
ans =

     3     4
     0     6
     7     9


In [21]:
J(:) % transforms the matrix into one long column
ans =

     3
     0
     7
     1
     1
     5
     4
     6
     9
     6
     8
     0
     2
     3
     4


In [22]:
J(2,end)
ans =

     3


In [23]:
J(end,4)
ans =

     0


In [24]:
J(end)
ans =

     4


Transpose

A' : Calculates the transpose of matrix A.

In [26]:
A
A =

     3    10    -1
     9     4     5
     6     2    -7


In [27]:
A'
ans =

     3     9     6
    10     4     2
    -1     5    -7


When used with complex numbers, the transpose operation returns the complex conjugate.

In [28]:
K=[1-i 6+3i; 8-5i 4+2i]
K =

   1.0000 - 1.0000i   6.0000 + 3.0000i
   8.0000 - 5.0000i   4.0000 + 2.0000i


In [29]:
K'
ans =

   1.0000 + 1.0000i   8.0000 + 5.0000i
   6.0000 - 3.0000i   4.0000 - 2.0000i


Dot Product and Cross Product

dot(X,Y) : Calculates the dot product of two vectors.

In [30]:
X=[-2 6 9];
Y=[3 -1 4];
dot(X,Y)
ans =

    24


In [31]:
dot(Y,X)
ans =

    24


cross(X,Y) : Calculates the cross product of two vectors.

In [32]:
X=[-2 6 9];
Y=[3 -1 4];
cross(X,Y)
ans =

    33    35   -16


In [33]:
cross(Y,X)
ans =

   -33   -35    16


Determinant and Repmat

det(A) : Calculates the determinant of matrix A.

In [34]:
det(A)
ans =

   822


repmat(A,m,n) : Creates a large matrix consisting of an m-by-n tiling of copies of A.

In [35]:
repmat(A,1,2)
ans =

     3    10    -1     3    10    -1
     9     4     5     9     4     5
     6     2    -7     6     2    -7


Diagonal Matrices

diag(A) : Extracts the diagonal of matrix A.

In [36]:
diag(A)
ans =

     3
     4
    -7


In [37]:
diag(A,1)
ans =

    10
     5


In [38]:
diag(A,2)
ans =

    -1


In [39]:
diag(A,-1)
ans =

     9
     2


In [40]:
diag(A,-2)
ans =

     6


For any vector D, diag(D) creates a square matrix with D as the diagonal.

In [41]:
D=[3 10 5];
diag(D)
ans =

     3     0     0
     0    10     0
     0     0     5


In [42]:
diag(D,1)
ans =

     0     3     0     0
     0     0    10     0
     0     0     0     5
     0     0     0     0


In [43]:
diag(D,-1)
ans =

     0     0     0     0
     3     0     0     0
     0    10     0     0
     0     0     5     0


blkdiag : Generates block diagonal concatenation of matrix input arguments.

In [44]:
E=[3 5]; F=[1 2; 3 4]; G=[8];
blkdiag(E,F,G)
ans =

     3     5     0     0     0
     0     0     1     2     0
     0     0     3     4     0
     0     0     0     0     8


In [45]:
blkdiag(G,F)
ans =

     8     0     0
     0     1     2
     0     3     4


fliplr and flipud

fliplr(H) : flips the matrix H into its mirror image, from right to left.

In [46]:
H=[1 0 0; 0 2 0; 0 0 3]
H =

     1     0     0
     0     2     0
     0     0     3


In [47]:
fliplr(H)
ans =

     0     0     1
     0     2     0
     3     0     0


flipud(H) : flips the matrix H vectically.

In [48]:
flipud(H)
ans =

     0     0     3
     0     2     0
     1     0     0


Magic Matrix

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.

In [49]:
magic(3)
ans =

     8     1     6
     3     5     7
     4     9     2


In [50]:
magic(4)
ans =

    16     2     3    13
     5    11    10     8
     9     7     6    12
     4    14    15     1


Inverse Matrix

inv(A) : Calculates the inverse of matrix A.

In [51]:
inv(A)
ans =

   -0.0462    0.0827    0.0657
    0.1131   -0.0182   -0.0292
   -0.0073    0.0657   -0.0949


rank(A) : Calculates the rank of matrix A.

In [52]:
rank(A)
ans =

     3


In [53]:
K=[1 2 1; -2 -3 1; 3 5 0]
K =

     1     2     1
    -2    -3     1
     3     5     0


In [54]:
rank(K)
ans =

     2


Solutions of Systems of Linear Equations

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:

In [55]:
M=[3 2 1; -1 3 2; 1 -1 -1]
M =

     3     2     1
    -1     3     2
     1    -1    -1


In [56]:
N=[10; 5 ; -1]
N =

    10
     5
    -1


In [57]:
X = inv(M)*N
X =

    2.0000
    1.0000
    2.0000


So the solution to the system is

$$X=\begin{bmatrix} 2 \\ 1 \\ 2 \end{bmatrix}$$