Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 34

ARRAYS AND MATRICES

by
Mohamed Hussein

compliments to

Prof. Michael Negnevitsky


Univerity of Tasmania
Lecture 2

Introduction to Matlab
 Array Mathematics
 Matrices and Matrix Manipulation
 Matrix Operations
Scalar-Array Mathematics
>> a = [1 2 3 4];
>> a - 2
ans =
-1 0 1 2
>> 2*a + 1
ans =
3 5 7 9
Array-Array Mathematics
 When arrays have the same length addition,
subtraction, multiplication and division apply
on an element-by-element basis.

>> a = [1 2 3 4];
>> b = [5 6 7 8];
>> c = a + b
c=
6 8 10 12
Array-Array Mathematics (cont.)
 Element-by-element multiplication uses dot
multiplication symbol .*

>> a = [1 2 3 4];
>> b = [5 6 7 8];
>> c = a.*b
c=
5 12 21 32
Array-Array Mathematics (cont.)
 Element-by-element division uses dot
division symbol ./

>> a = [1 2 3 4];
>> b = [5 6 7 8];
>> c = a./b
c=
0.2 0.3333 0.4286 0.5
Array Orientation
 Row vector
>> a = [1 2 3 4]
a=
1 2 3 4
 Column vector
>> b = [1; 2; 3; 4]
b=
1
2
3
4
Array Orientation (cont.)
 The Matlab transpose operator (‘) changes the
row vector into the column vector
>> a = [1 2 3 4]
a=
1 2 3 4
>> b = a’ or >> b = [1 2 3 4]'
b=
1
2
3
4
Matrices and Matrix Manipulation
 An array can be used to represent a vector. For
example, a vector q=3i + 4j + 12k is represented in
MATLAB as [3 4 12] or [3,4,12] . Understand this
concept can help to solve a lot of mathematical
problems
 Arrays having multiple rows and columns are called
matrices. A 3x2 matrix is an array with 3 rows and
2 columns. MATLAB displays rows horizontally
and columns vertically
Matrix Manipulation (cont.)
 Spaces are used to separate elements in a specific row, and
semicolons are used to separate individual rows:
>> A = [1 -2;3 4] or >> A = [1, -2;3, 4]
A=
1 -2
3 4

 Matrix element is addressed using a bracket. A(1,2) refers to the


element in first row, column two:
>> A (1,2)
ans =
-2
Matrix Manipulation (cont.)
>> A = [1 2 3;4 5 6]
A=
1 2 3
4 5 6

>> A(1,3) = 0

A=
1 2 0
4 5 6

Changes the element in the first row and third column to zero.
Matrix Manipulation (cont.)

>> A(4,3) = 1
A=
1 2 0
4 5 6
0 0 0
0 0 1

Places one the in the fourth row and third column. Since A does
not have four rows, the size of a is increased as necessary and
filled with zeros so that the matrix remain rectangular.
Matrix Manipulation (cont.)
>> A =[1 2 3;4 5 6];
>> B = A(2,:)
B=
4 5 6
>> B = A(1:2,1:2)
B=
1 2
4 5
>> B = A(2:-1:1,:)
B=
4 5 6
1 2 3
Creates matrix B by taking the rows of A in reverse order. The final
single colon means “take all columns”.
Matrix Manipulation (cont.)

>> C = [A B] or >> C = [A, B]


C=
1 2 3 4 5 6
4 5 6 1 2 3

>> D = [A;B]
D=
1 2 3
4 5 6
4 5 6
1 2 3
Matrix Manipulation (cont.)
>> A = [1 2 3;4 5 6];
>> B = A(:)
B=
1
4
2
5
3
6
builds B by stretching A into a column vector by
taking its columns one at a time.
Matrix Manipulation (cont.)
>> A = [1 2 3;4 5 6];
>> B = A
B=
1 2 3
4 5 6
>> B (:,2) = [ ]
B=
1 3
4 6

redefines b by throwing away all rows in the second column of


original B. [ ] is the empty matrix.
Matrix Manipulation (cont.)
>> A = [1 2 3;4 5 6];
>> B = A
B=
1 2 3
4 5 6

>> B (2,:) = [ ]
B=
1 2 3

throws out all column in the second row of original B.


Matrix Manipulation (cont.)
>> A = [1 2 3;4 5 6];
>> B = [7 8 9]
B=
7 8 9

>> A(2,:) = B
A=
1 2 3
7 8 9

replaces the second row of A with B.


Matrix Manipulation (cont.)
>> A = [0.1 -2 3;0.9 -0.5 4]
A=
0.1000 –2.0000 3.0000
0.9000 –0.5000 4.0000

>> B = [abs(A)>1]
B=
0 1 1
0 0 1

creates B by giving ones where the absolute value of a is greater


than 1.
Matrix Manipulation (cont.)
>> A = -4:4
A=
–4 –3 –2 –1 0 1 2 3
4
>> B = find(abs(A)>1)
B=
1 2 3 7 8 9
Matlab includes function find that returns the subscripts where
a relational expression is True.

>> C = A(B)
C=
–4 –3 –2 2 3 4
>> A = [1 2 3;4 5 6]
A=
1 2 3
4 5 6

>> [x,y]=find(A>4)
x=
2
2
y=
2
3
Here the indices stored in x and y are the row and column indices, where
the relational expression is True. Elements are scanned by columns.
Try:
A=[1 2 3;3 4 5;5 6 7]
Matrix Manipulation (cont.)
>> A = [1 2 3;4 5 6]
A=
1 2 3
4 5 6
>> x = size(A)
x=
2 3
The size function returns a row vector whose
first element is the number of rows and whose
second element is the number of columns.
More on Array and Matrix
find(x) – computes the indices on non
zero elements of the array x
logspace(a,b,n) - creates an array of n
logarithmically spaced between a and b
max(A) - returns algebraically largest
element (if A is an array) or a row array
containing the largest element (if A is a
matrix). Will look for the largest magnitude if
complex elements exist
min(A) - similar to max(A) but returns
minimum values
sort(A) - sorts each column of array A in
ascending order and returns an array the same
size as A
Matrix Scalar Operation
To increase the value of each element of a matrix, direct scalar
addition, subtraction, multiplication or division can be used
>> P = [2 3 8; 5 4 6];
>> Q = 10+P
Q=
12 13 18
15 14 16

>> Q = P-2
Q=
0 1 6
3 2 4

How about Q = 2-P ? Let’s try…


Matrix Scalar Operation (cont.)
>> P = [2 3 8; 5 4 6];
>> Q = 10*P
Q=
20 30 80
50 40 60

>> Q = P/2
Q=
1.0000 1.5000 4.0000
2.5000 2.0000 3.0000

How about Q = 2/P or Q = P^2 ?


Try Q = P.^2
Matrix Element by Element Operation
MATLAB regards element by element matrix operations as array
operations. For element by element operations, the arrays (matrices)
involved must have the same sizes. The following shows element by
element addition, subtraction, multiplication and division of two arrays.

>> A = [1 2 3; 10 11 12]; B=[7 8 9; 4 5 6];


>> A + B
ans =
8 10 12
14 16 18
>> A - B
ans =
-6 -6 -6
6 6 6
Matrix Element by Element Operation
(cont.)
>> A = [1 2 3; 10 11 12]; B=[7 8 9; 4 5 6];
>> A .*B
ans =
7 16 27
40 55 72

>> A ./ B
ans =
0.1429 0.2500 0.3333
2.5000 2.2000 2.0000

How about A .\ B?
Matrix Element by Element Operation
(cont.)

>> A = [1 2 3; 10 11 12]; B=[7 8 9; 4 5 6];


>> A .^B
ans =
1 256 19683
10000 161051 2985984
Matrix Operations
Product (multiplication) of two matrices AB is obtained using ‘*’
operation (not ‘.*’). The number of columns in A must equal to the
number of rows in B. The result of the product is a matrix with the same
number of rows as A and the same number of columns as B i.e if matrix
A has a size of m  n and B has a size of n  p, the product will be a
matrix of size m  p

>> A = [1; 2; 3]; B=[4, 5, 6];


>> A*B >> B*A
ans = ans =
4 5 6 32
8 10 12
12 15 18
Matrix Operations
>> A = [1,2,3; 4,5,6]; B=[4, 5;6,7;8,9];
>> A*B >> B*A
ans = ans =

40 46 24 33 42
94 106 34 47 60
44 61 78
ASSIGNMENT
Four weekend Programming classes consist of students with the following numbers:
Class I=32 students, Class II=40 students, Class III=34 students and Class IV=28
students. The distribution of students’ average marks for each class for four (4) tests is
given as the followings:

Test1 = [76 78 68 82]


Test2 = [58 65 70 66]
Test3 = [82 84 75 90]
Test4 = [77 68 69 75]

For the above distribution, write a single SciLab/Matlab command to determine:


1. Number of students attended the Programming class
2. Total student marks for Test 1
3. Total Test2 mark for Class III
4. Total student Test3 mark for each class
5. Which class has an average mark of Test4 above 70
6. Minimum average mark of Test4
Matrix Operations

 Matrix division uses both right and left division ‘/’ and ‘\’ not ‘./’ and
‘.\’) while matrix exponentiation for example A2 = AA can be
obtained by typing A^2 (not A.^2). However A should be a square
matrix (which has the same number of rows and columns).

 Matrix cross product; A x B and matrix dot product; A.B can be


comupute using cross product and dot product functions which are
cross(A,B) and dot(A,B) respectively.
Special Matrices
>> a = zeros(2)
a=
0 0
0 0
a 2-by-2 matrix of zeros.
>> a = ones(2,3)
a=
1 1 1
1 1 1
a 2-by-3 matrix of ones.
Special Matrices (cont.)

>> a = eye(3)
a=
1 0 0
0 1 0
0 0 1
a 3-by-3 identity matrix.

You might also like