arrays & matices

16
by Mohamed Hussein INTRODUCTION TO MATLAB compliments to Prof. Michael Negnevitsky Univerity of Tasmania Lecture 2 Introduction to Matlab Array Mathematics Matrices and Matrix Manipulation Matrix Operations

Upload: azlan

Post on 07-Dec-2015

215 views

Category:

Documents


2 download

DESCRIPTION

ar

TRANSCRIPT

Page 1: Arrays & Matices

byMohamed Hussein

INTRODUCTION TO MATLAB

compliments to

Prof. Michael NegnevitskyUniverity of Tasmania

Lecture 2

Introduction to Matlab

Array MathematicsMatrices and Matrix ManipulationMatrix Operations

Page 2: Arrays & Matices

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-be-element basis.

>> a = [1 2 3 4];>> b = [5 6 7 8];>> c = a + bc =

6 8 10 12

Page 3: Arrays & Matices

Array-Array Mathematics (cont.)

Element-by-element multiplication uses dot multiplication symbol .*

>> a = [1 2 3 4];>> b = [5 6 7 8];>> c = a.*bc =

5 12 21 32

Array-Array Mathematics (cont.)

Element-be-element division uses dot division symbol ./

>> a = [1 2 3 4];>> b = [5 6 7 8];>> c = a./bc =

0.2000 0.3333 0.4286 0.5000

Page 4: Arrays & Matices

Array OrientationRow vector>> a = [1 2 3 4]a =

1 2 3 4Column vector>> b = [1; 2; 3; 4]b =

1234

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 =

1234

Page 5: Arrays & Matices

Matrices and Matrix ManipulationAn 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 problemsArrays 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 verticallySpaces 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 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

Array element is addressed using a bracket. a(1,2) refers to the element in first row, column two:>> A (1,2)ans =

-2

Page 6: Arrays & Matices

Matrix Manipulation (cont.)

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

1 2 34 5 6

>> A(1,3) = 0A =

1 2 04 5 6

Changes the element in the first row and third column to zero.

Matrix Manipulation (cont.)>> A(4,3) = 1A =

1 2 04 5 60 0 00 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.

Page 7: Arrays & Matices

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 61 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 64 5 6 1 2 3

>> D = [A;B]D =

1 2 34 5 64 5 61 2 3

Page 8: Arrays & Matices

Matrix Manipulation (cont.)>> A = [1 2 3;4 5 6];>> B = a(:)B =

142536

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 = AB =

1 2 34 5 6

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

1 34 6

redefines b by throwing away all rows in the second column of original B. [ ] is the empty matrix.

Page 9: Arrays & Matices

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

1 2 34 5 6

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

1 2 3throws out 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,:) = BA =

1 2 37 8 9

replaces the second row of A with B.

Page 10: Arrays & Matices

Matrix Manipulation (cont.)

>> A = [0.1 -2 3;0.9 -0.5 4]A =

0.1000 –2.0000 3.00000.9000 –0.5000 4.0000

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

0 1 10 0 1

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

Matrix Manipulation (cont.)>> A = -4:4A =

–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

Page 11: Arrays & Matices

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

1 2 34 5 6

>> [x,y]=find(A>4)x =

22

y = 23

Here the indices stored in x and y are the row andcolumn indices, where the relational expression is True.

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

1 2 34 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.

Page 12: Arrays & Matices

More on Array and Matrixfind(x) – computes the indices on non zero elements of

the array xlogspace(a,b,n) - creates an array of n logarithmically spaced

between a and bmax(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 valuessort(A) - sorts each column of array A in ascending

order and returns an array the same size as Asum(A) - sums the elements in each column of array A

and returns a row vector containing the sums

Matrix Scalar OperationTo 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 1815 14 16

>> Q = P-2Q =

0 1 63 2 4

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

Page 13: Arrays & Matices

Matrix Scalar Operation (cont.)>> P = [2 3 8; 5 4 6];>> Q = 10*P

Q = 20 30 8050 40 60

>> Q = P/2Q =

1.0000 1.5000 4.00002.5000 2.0000 3.0000

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

Matrix Element by Element OperationMATLAB 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 1214 16 18

>> A - Bans =

-6 -6 -66 6 6

Page 14: Arrays & Matices

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 2740 55 72

>> A ./ Bans =

0.1429 0.2500 0.33332.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 .^Bans =

1 256 1968310000 161051 2985984

Page 15: Arrays & Matices

Matrix OperationsProduct (multiplication) of two matrices AB is obtained using ‘*’operation (not ‘.*’). The number of columns in A must equal to thenumber of rows in B. The result of the product is a matrix with the samenumber of rows as A and the same number of columns as B i.e if matrixA has a size of m × n and B has a size of n × p, the product will be amatrix of size m × p

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

4 5 6 328 10 12

12 15 18

Matrix Operations

Matrix division (multiplication) 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 asquare matrix (which has the same number of rows and columns).

Matrix cross product; A x B and matrix dot product; A.B can becomupute using cross product and dot product functions which arecross(A,B) and dot(A,B) respectively.

Page 16: Arrays & Matices

Special Matrices>> a = zeros(2)a =

0 00 0

a 2-by-2 matrix of zeros. >> a = ones(2,3)a =

1 1 11 1 1

a 2-by-3 matrix of ones.

Special Matrices (cont.)

>> a = eye(3)a =

1 0 00 1 0 0 0 1

a 3-by-3 identity matrix.