You are on page 1of 50

M ATRICES

The array is the fundamental form


that MATLAB uses to store and
manipulate data.

An array is a list of numbers arranged


in rows and/or columns.
CREATING A ONE-DIMENSIONAL
ARRAY

• Frequently represents vectors


• A list of numbers that is placed in a
row or column
• In MATLAB, a vector is created by
assigning the elements of the vector
to a variable
Creating a vector from a known list
of elements
Syntax: var_name = [type vector elements]

Row vector: Type elements with a space or comma between


the elements inside the square brackets
row = [ 1 2 3,4]

Column vector: Type the left square bracket [ and then enter
the elements with a semicolon between them, or press
the ENTER key after each element. Type the right square
bracket ] after the last element.
col = [1;2;3;4]
Creating a vector with constant
spacing
Syntax: var_name = [m:q:n]
or var_name = m:q:n
where m is the first term
q is the spacing
n is the last term
Examples:
>> x = [1:2:13]
>> y = [1.5:-0.1:1]
>> z = [-3:7]
Creating a vector with constant
spacing given the number of terms
Syntax: var_name = linspace(xi,xf,n)
where xi is the first element
xf is the last element
n is the number of elements
Examples:
>> va = linspace(0,8,6)
>> vb = linspace(30,10,11)
CREATING A TWO-DIMENSIONAL
ARRAY
• Also called a matrix, has numbers in rows
and columns
• Size of a matrix: mxn
where m is the number of rows
n is the number of columns
• Created by assigning the elements of a
matrix to a variable
Syntax: var_name = [ 1st row elements; 2nd
row elements; 3rd row elements; …;last row
elements]
Examples:
>> a = [5 53 43; 4 7 78; 35 67, 8]
>> b = [ 2 4 6 7 8
3 4 67 2 9
23 4 5 6 34]

Note: all rows must have the same number


of elements
The elements that can be entered can be
numbers or mathematical expressions that
may include numbers, predefined variables
and functions.

Examples:
>> cd = 6; e = 3; h = 4;
>> Mat = [e, cd*h, cos(pi/3);h^2,
sqrt(h*h/cd), 14]
Rows of a matrix can also be entered as
vectors using the notation for creating
vectors with constant spacing or the
linspace command.

Example:
>> A = [1:2:11;0:5:25;linspace(10,60,6);
67 2 43 68 4 13]
REVIEW
The MATRIX in MATLAB
Columns (n) A(2, 4)
1 2 3 4 5
16
A= 1 45
1
3.4
6
-8
11
3.23 1
21
A(17)
2 7 12 17 22
2 56 76 2.34 4 2
Rows 3 3 8 13 18 23
(m)
273 21 4.5 2.56 2.06
4 9 14 19 24
4 78 2.12 98 -4.67 2.45
5 10 15 20 25
5 2 23 7 1 0.2
ENTERING NUMERIC ARRAYS
Use square
brackets
Column separator
semi-colon
Row separator
Creatingspace/comma
sequences
using the colon operator

Utility function for


creating matrices

Loading pre-existing
variables using LOAD
ENTERING NUMERIC ARRAYS
Using other
MATLAB
expressions
Matrix element
assignment

Matrices must be
rectangular. Undefined
elements set to zero
The MATRIX in MATLAB

Scalar: 1-by-1 array


Vector: m-by-1 array
n-by-1 array
Matrix:m-by-n array
MATRIX
OPERATIONS
Matrix Operations
( ) parentheses
‘ comp. transpose
^ power
* multiplication
/ division
\ left division
+ addition
- subtraction
Matrix Addition and Subtraction
If A and B are two matrices having the same
order, then
• the sum of A and B, denoted by A + B, is
the matrix for which each of its elements is
the sum of the corresponding elements of
A and B.
• the difference of A and B, denoted by
A – B, is defined by
A – B = A + (-B)
Example:

5 -2 1  4 7 6
A=   and B =  
3 0 -4  -1 8 -3

5+4 -2+7 1+6  9 5 7 


A B=    2 8  7 
 3-1 0+8 -4-3  
 54 27 1 6 
A B   
3    1 0  8  4    3  
1 -9 -5 
A B=  
 4 -8 -1
Matrix Multiplication
When performing matrix multiplication,
these rules apply:
• Inner dimensions must be equal
• Dimensions of the resulting matrix are
the outermost dimensions of
multiplied matrices
Example: Given
 2 -3
 3 0 -4 
A=   and B = 4 -1 
 -2 2 -1   
1 5 
2  3
3 0  4  
AB    4  1
 2 2  1 
 1 5 

  3  2   0  4     4 1  3   3    0   1    4  5  
 
  2 2   2 4     11   2  3    2  1    1 5  
2 -29  12 -6 -5 
AB =   
 BA = 14 -2 -15
3 -1   

 7 10 -9 

Use MATLAB to get the product of the


matrices
Matrix Division
The division operation is associated with
the rules of linear algebra. MATLAB has
two types of array division: the right and
the left division.
The left division ( \ ) is used to solve
the matrix equation AX = B.
The right division ( / ) is used to solve
the equation XC = D.
Example:
Solve the system of linear equations:
3 x  4 y  8z  7

 4 x  y  6 z  1
x  8 y  z  10

Rewrite in matrix form: (3)(x)+(-4)(y)+(8)(z)=7
3  4 8   x   7 
4 1  6  y    1 or
    
 1 8 1   z  10 
3 4 1
 x y z  4 1 8   7  1 10
 8  6 1
Solving the form AX = B:
>> A = [ 3 -4 8; 4 1 -6; 1 8 1 ];
>> B = [ 7; -1; 10 ];
>> Xa = A\B

Solving the form XC = D:


>> C = [ 3 4 1; -4 1 8; 8 -6 1 ];
>> D = [ 7 -1 10 ];
>> Xc = D/C
Array Operations

.‘ comp. transpose
.^ power
.* multiplication
./ division
Scalar Addition
MATLAB allows the addition, subtraction,
multiplication and division of scalars to
matrices of any size (known as scalar
expansion).

>> w = [ 1 2; 3 4 ] + 5
Array Multiplication
When using array multiplication (.*), the
following rules apply:
• Matrices must have the same
dimensions
• Dimensions of resulting matrix are the
dimensions of the multiplied matrices
• Resulting elements are the product of
corresponding elements
Type the following in the command window:

>> a = [ 1 2 3 4; 1 2 3 4 ]
>> b = [ 1: 4; 1:4]
>> c = a.*b

Note: Other element-by-element


operations are (.^), (./) and (.\).
Common Matrix Functions

inv( ) matrix inverse


det( ) determinant
rank( ) matrix rank
eig( ) eigenvectors & values
svd( ) singular value dec.
norm( ) matrix/vector norm
Determinant of a Matrix
• A determinant is a scalar computed from
the entries in a square matrix.

• In MATLAB, the function det computes


the determinant of a square matrix.
Example:

For a 2 x 2 matrix:

>> A [1 3;-1 5]
A=
1 3
-1 5
>> det(A)
ans =
8
Example:

For a 3 x 3 matrix:

>> A [1 3 0;-1 5 2;1 2 1]


A=
1 3 0
-1 5 2
1 2 1
>> det(A)
ans =
10
Systems of Linear Equations by Cramer’s Rule

Example: Solve for x, y, z In the command window,


Given: just type:
3 – 4 8  >> D = [3 –4 8;4 1 –6;1 8 1]
3x –4y + 8z = 7 D = 4 1 – 6 >> Dx = [7 –4 8;- 1 1 –6;10 8 1]
 
4x + y – 6z = -1 1  8 1  >> Dy = [3 7 8;4 -1 –6;1 10 1]

x + 8y + z = 10  7 - 4 8
  >> Dz = [3 -4 7;4 1 –1;1 8 10]
Dx = - 1 1 - 6
10 8 >> x = det(Dx)/det(D)
1
3 7 8  >> y = det(Dy)/det(D)
   3 – 4 7
Dy = 4 – 1 – 6 4 1 - 1 
1 10 1  Dz =  
>> z = det(Dz)/det(D)
1 8 10 
Systems of Linear Equations

[A,B] = equationsToMatrix([eq1, eq2, eq3], [x, y, z])


A
= B=
3 – 4 8  
4 1 – 6 7
  -1
1 8 1 
10
SOME USEFUL ARRAY
FUNCTIONS
Functions Descriptions
magic(n) creates an n x n matrix where the
sum of its diagonal, rows and
columns are equal
eye(n) creates an n x n identity matrix
ones(n) creates an n x n matrix of ones
ones(m,n) creates an m x n matrix of ones
zeros(n) creates an n x n matrix of zeros
zeros(m,n) creates an m x n matrix of zeros
size(A) returns a row vector [m, n]
containing the sizes of the m x n
array A
length(A) computes either the number of
elements of A if A is a vector or the
largest value of m or n if A is an m x
n matrix
sum(A) sums the elements in each column
of the array A and returns a row
vector containing the sums
sort(A) sorts each column of the array A in
ascending order and returns an
array the same size as A
max(A) returns the algebraically largest
element in A if A is a vector.
Returns a row vector containing
the largest elements in each
column if A is a matrix
min(A) same as max(A) but returns
minimum values
diag(A) returns the main diagonal of A
Generating “Special” Matrices
Identity Matrix
eye(m,n) returns an m x n rectangular
identity matrix
eye(n) returns a n x n square identity matrix

>> A = eye (3)


>> B = eye(3,2)
>> C = [ 1 2 3 ; 4 2 6 ]
>> D = eye(size(C))
Try creating:

1. 4 x 4 all zeros-matrix
2. 3 x 4 all ones-matrix
3. 5 x 4 all five-matrix
4. 4 x 4 magic square
To create a 4 x 4 magic square:

In the command window, just type:


>> magic(4)

16 2 3 13
17 11 10 8
9 7 6 12
4 14 15 1
Transpose of a Matrix
The transpose of a matrix is a new matrix in
which the rows of the original matrix are the
columns of the new matrix.

In the command window, just type:


>> A=magic(4)
>> B=A’
Inverse of a Matrix
By definition, the inverse of a square matrix A
is the matrix A-1 such that the matrix products AA-1
and A-1A are both equal to the identity matrix.
Not every matrix has an inverse. A matrix has
an inverse only if it is square and its determinant is
not equal to zero.
In the command window, just type:
>> A=rand(4) Check:
>> A*B
>> B=inv(A) >> B*A
Numerical Array Concatenation
You can join matrices using square
brackets, which is the array concatenation
operator. Within the brackets, a
space/comma is used to separate rows;
while a semicolon is used to separate
columns
>> a = [ 1 2; 3 4 ]
>> cat_a = [ a, 2*a; 3*a, 4*a; 5*a, 6*a]
Array Subscripting / Indexing
[ ]Use
to:end
create
colon
parentheses
specifies
separator
matrixmax
of
( ) index
to A(2, 4)
specifies
specify
matrix
subscript
range
index.
value
/ALL.
A(17)
1 2 3 4 5 A(1:5,5)
A= 1
4 3 8 3 1 A(:,5)
A(:,end,end)
2 6 7 4 4 2 A(:,end)
3 3 1 5 2 5 A(21: 25)
4 8 2 8 7 4 A(21:end)

5 2 3 7 1 8 A(4:5,2:3)
A([9 14;10 15])
ARRAY ADDRESSING/INDEXING
Array indices are the row and column
numbers of an element and they are used to
keep track of the array’s elements.
Illustration:
Address Description
A(2) refers to 2nd element in the vector v
A(3,4) refers to the element in row 3,
column 4
A(:) represents all the row or column
elements of the vector v
A(1:3) represents the 1st through 3rd
elements of v
A(:, 2) denotes all the elements in the 2nd
column of A
A(:, 1:4) denotes all the elements in the 1st
through 4th column of A
A(3, :) denotes all the elements in the 3rd
row of A
A(2:4, :) denotes all the elements in the 2nd
through 4th row of A
A(2:3,1:3) denotes all the elements in the 2nd
through 3rd row that are also in the
1st
Example of Programming:
>> A=rand(4,4) % creates a 4 x 4 matrix A
>> col1=A(:,1) % stores the elements of
the 1st column of A to col1
>> row2=A(2,:) % stores the elements of the 2nd
row of A to row2
>> [sorted,i] = sort (col1)
% sorts the elements of col1
from lowest to highest
>> sorted = col1(i)
% arranges the elements from
highest to lowest
Utilize helpwin
elmat.

You might also like