1 / 87

4.1 BASICS

4.1 BASICS. Setting Up Matrices. Assignment a matrix A = [ {row 1}; {row 2}; ……; {row m} ] Ex: To set up a 4x4 matrix A ,like A = [1 1 1 1;1 2 3 4;1 3 6 10;1 4 10 20] It’s also legal to make input ‘look like’ output. A = [1 1 1 1 1 2 3 4 1 3 6 10 1 4 10 20 ].

ivie
Download Presentation

4.1 BASICS

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 4.1 BASICS

  2. Setting Up Matrices • Assignment a matrix A = [ {row 1}; {row 2}; ……; {row m} ] Ex: To set up a 4x4 matrix A ,like A = [1 1 1 1;1 2 3 4;1 3 6 10;1 4 10 20] • It’s also legal to make input ‘look like’ output. A = [1 1 1 1 1 2 3 4 1 3 6 10 1 4 10 20 ]

  3. Simple Output • If you enter A = [1 2;3 4] without a semicolon ,then the system responds with • If you already assignment A wit a semicolon, but now you want to display it . You could only type A to display.

  4. Types and Dimension • Complex syntax c = complex(a,b), it means c = a + bi • Dimension is handled automatically in MATLAB. Suppose you set B = [1 2 3; 4 5 6 ], and then set B = [1 0; 0 1]. The system ‘knows enough’ to recognize that B has changed from 2-by-3 matrix to a 2-by-2 matrix.

  5. Subscripts • The subscripts in MATLAB must be positive. • If you type A(1.2 , 3.5) = 10, you will get a error message . ?? Subscript indices must either be real positive integers or logicals

  6. Vectors and Scalars • Vector and scalar are ‘special matrices’. The commands, x = [ 1 ; 2 ; 3 ] ; or x = [1 2 3]’ ; establish x as a column vector while • Assignment an 1-by-1 matrix with c = [3], and it equals to c = 3 .

  7. Size and Length If we declare A = [ 1 2 ; 3 4 ; 5 6 ], v = [7 8 9 10] • Size & Length syntax size(A) ans = [3 2] length(v) ans = 4

  8. Continuation • Sometimes it is not possible to put an entire MATLAB instruction on a single line. We can break the instruction in a ‘natural’ place. • There are some ways to express a instruction, and they are the same. A = [1 2 3; 4 5 6 ;....... 7 8 9] A = [1 2 3; 4 5 6 ; 7 8 9] A = [1 2 3; 4 5 6 ; 7 8 9]

  9. Variable Names • Variable names in MATLAB must consist of nineteen or fewer characters. • The name must begin with a letter.Any letter, number, underscore may follow, e.g., sym_part1. • Upper and lower cases are distinguished in MATLAB.

  10. Addition, Multiplication, Exponentiation • If A and B are matrices the C = A+B sets C to be the sum while C = A*B is the product. • To raise a square A to power r enter C = A^r • C = A^(-1) assigns the inverse of A to C. • f = c1Ab + c2A2b + c3A3b f = A*(A*(c(3)*A*b+c(2)*b)c(1)*b) f = ( c(3)*A^3+ c(2)*A^2+c(1)*A )*b

  11. Transpose • The conjugate transpose of a matrix can be obtained using a single quote: Set B = At B = A’

  12. The “Colon Method” for Setting Up Vectors • Colons can be used to prescribe vectors whose components differ by a fixed increment. • v = 1:3 v = [1 2 3] • v = 3:-1:0 v = [3 2 1 0] • v = 1:2:10 v = [1 3 5 7 9]

  13. Logarithmically Spaced Vectors • LOGSPACE logspace(a,b,n) generates n points between decades 10^a and 10^b. If w = logspace(d1,d2,n), then w(i) = 10 ^ [ d1 +(i-1)(d2-d1)/(n-1) ] • w = logspace(0,3,4) w = [1 10 100 1000]

  14. Generation of Special Matrices • MATLAB has a number of build-in functions that can be used to generate frequently occurring matrices: A = eye(m , n ) A m-by-n, ones on diagonal, zeros elsewhere. A = zeros(m , n ) A m-by-n, zeros everywhere. A = ones(m , n ) A m-by-n, ones everywhere.

  15. Random Matrices • It’s possible to generate matrices and vectors of random numbers: A = rand(m , n ) A m-by-n, random element between 0 and 1.

  16. Complex Matrices • If A and B are matrices with the same dimension and i2=-1, then the complex matrix C = A+iB can be generated as follows: C = A + sqrt(-1) * B ; or C = A + i * B ;

  17. Pointwise Operations • Element operations between two matrices of the same size are also possible: C = A.*B cij = aij * bij C = A.\B cij = bij / aij C = A./B cij = aij / bij C = A.^B cij = aijbij C = A.’ cij = aji • If we have a=[1 2], then type a.*[3 4] We get the answer = [1*3 2*4 ]= [3 8].

  18. More On Input/Output I • The format for printed output can be controlled using the following commands: format short 5-digit fixed point style format short e 5-digit floating point style format long 15-digit fixed point style format long e 15-digit floating point style format hex hexadecimal

  19. More On Input/Output II • format hex hexadecimal To represent a number into double precise floating point (with hexadecimal form). Ex. To show 1 in format ‘ hex ’ in matlab 1 = + 1.0 * 2^0 sign exponent+1023 mantissa 0011 1111 1111 0000 0000 0000 ..... 0000 3 f f 0 0 0 ..... 0 0+1023

  20. Machine Precision • At the start of MATLAB session, the variable eps contains the effective machine precision,i.e.,the smallest floating point number such that if x = 1 + eps then x>1 . • eps = 2.220446049250313e-016

  21. Flops • This is an obsolete function. • It can estimate the count of the program. • For complex operands, addition and subtraction count as two flops while multiplications and divisions each count as six flops.

  22. 4.2 LOOPS AND CONDITIONALS

  23. For-Loops • Syntax for {var} = {row vector of counter values} {statements} end • Ex: for i = 1:3 x(i) = i; end is equivalent to x = [1;2;3];

  24. Relations • Relation operators == equals ~= not equals < less than <= less than or equal to > greater than >= greater than or equal to • Ex: A=[1 2;3 4]; B=[1 2;2 4]; T = A==B Then, the output is T = 1 1 0 1

  25. ‘and’, ’or’, ’not’ • And(&), or(|), not(~) The operations are possible between 0-1 matrices of equal dimension. • If T1 = [1 1 ; 0 1] ,T2 = [1 0 ; 0 0] T=T1&T2, T= 1 0 0 0 T=T1|T2, T= 1 1 0 1 T=~T1, T= 0 0 1 0

  26. ‘if’ Constructions • Syntax if {relation} {statements} end • Ex:Set a upper triangular matrix that has 1’s on the diagonal and –1’s above the diagonal. for i = 1 : n for j = 1 : n if i < j A(i,j) = -1; elseif i > j A(i,j) = 0; else A(i,j) = 1; end end end

  27. The ‘any’ and ‘all’ Functions • Any If any component of the vector is nonzero, it returns a “1”. • All If all the entries are nonzero, it returns a “1”. • a = [ 0 1 1; 0 0 1; 0 0 1] any(a) ans =[0 1 1] all(a) ans =[0 0 1]

  28. While-Loops • Syntax while {relation} {statements} end • Print a sequence{Ai} of random 2-by-2 with the property that Ai+1>Ai: A = zeros(2) B = rand(2,2) while(B>A) A=B B=rand(2,2) end

  29. The ‘break’ Command • Break It can be used to terminate a loop. • Print all Fibonnaci number less than 1000: fib(1) = 1 fib(2) = 1 for j = 3:1000 z = fib(j-1) + fib(j-2) if z >= 1000 break end fib(j) = z end

  30. 4.3 WORKING WITH SUBMATRICES

  31. Setting Up Block Matrices • We can set up the block matrix as follows: A=[A11 A12 ; A21 A22 ; A31 A32 ] • If we want to make a 6-by-6 matrix with random 2-by-2 block diagonal, like A = rand(2,2) for i = 2:3 [m,m] = size(A); A = [ A zeros(m,2) ; zeros(2,m) rand(2,2) ] end

  32. The Empty Matrix • The empty matrix [] is often useful for initialization of certain matrix computations. B=[] B is empty • To set up a column-reversed version of above example: B = [] for j = 6:-1:1 B = [ B A(:,j) ]; end

  33. Designating Submatrices • If A is m-by-n matrix and i,j,p,q are integers. Then, B= A( i:j , p:q ) B= • B = A ( : , p:q ), the range ( : ) means from 1:n, if the dimension of matrix is n.

  34. Assignment to Submatrix If A = , we can assign the column 3 to [3 3 3]’ by using A(:,3)=3 or A ([7 8 9])=[3 3 3]. Then A =

  35. Kronecker Product • The Kronecker product of two matrices A and B can be calculated using the build-in function kron: C = kron(A,B) C = (aijB) • C = []; [m,n] = size(A); for j = 1:n ccol = []; for i = 1:m ccol = [ ccol ; A(i,j)*B ]; end C = [C ccol] end • If C = kron( [ 1 2 ] , [ 1 2 ; 3 4 ] ) then C = 1 2 2 4 3 4 6 8

  36. Turning Matrices into Vectors and Vice Versa • If A is a m-by-n matrix and we want to set v is an mn-by-1 vector. v = A(:) • Set a transpose of a 2-by-2 matrix A = [ 1 2 ; 3 4 ] x = A(:); x( [2 3] ) = x( [3 2] ); A(:) = x;

  37. 4.4 BUILD-IN FUNCTIONS

  38. ‘abs’, ‘real’, ‘imag’, ‘conj’ • B = abs(A) B = ( |aij| ) B = real(A) B = ( real(aij) ) B = imag(A) B = ( imag(aij) ) B = conj(A) B = real(A)-i*imag(A), i2=-1 • If A=[1-2i 2-i;3-4i 4-3i]; conj(A); Then, ans = 1+2i 2+i 3+4i 4+3i • MATLAB’s build-in functions are in lower case, unless user define. Otherwise, it’s will show ‘Capitalized internal function XXXX; Caps Lock may be on.’

  39. Norms • t = norm(A) t = || A ||2 t = norm(A,1) t = || A ||1 t = norm(A,’inf’) t = || A || t = norm(A,'fro') t = || A ||F • If A = [1 2 3 4] norm(A) ans = 5.47722557505166 sqrt(1+4+9+16) ans = 5.47722557505166

  40. Largest and Smallest Entries • If v = max(A) v = [ max(A(:,1)), … , max(A(:,n)) ] [v,i] = max(A) v = [ max(A(:,1)), … , max(A(:,n)) ] i = [the row of the max value in] • A =[ 1 2 8 6 9 4 7 5 3]; and [v,i]=max(A); v= 7 9 8 i = 3 2 1

  41. Sums and Products • t = sum(A) t = t = prod(A) t = • A =[ 1 2 9 6 9 4 7 5 3] sum(A) ans =14 16 15 prod(A) ans =42 90 96

  42. ‘sort’ and ’find’ I • Sorts each column of A in ascending order by using sort(A) • [v,i] = sort(A), returns in sorted v and an integer vector i which indicate the permutation of new from old matrix. • A =[ 3 5 9 2 6 8 1 4 7] [B,i]=sort(A); B =[1 4 7 2 5 8 3 6 9] i =[ 3 3 3 2 1 2 1 2 1]

  43. ‘sort’ and ’find’ II • The find function can be used to locate nonzero entries. • A =[1 0 2 3 0] find(A) ans =[1 3 4] find(A == 0) ans =[2 5]

  44. Rounding, Remainders, and Signs I • There are several conversion-to-integer routines: round(x) round x to nearest integer fix(x) round x to zero floor(x) round x to - ceil(x) round x to + • A = [ 1.1 -2.2 -0.9 3.8 ] round(A) ans=[1 - 2 -1 4] fix(A) ans=[1 -2 0 3] floor(A) ans=[1 -3 -1 3] ceil(A) ans=[2 -2 0 4]

  45. Rounding, Remainders, and Signs II • t = sign(x) t = • t = rem(x,y) t = x-y*fix(x/y) If y = 0, rem(x,0) is NaN. And x and y must be the same dimension. • rem equals mod(x,y) if x and y are the same sign. Otherwise , mod(x,y) = rem(x,y)+y a = mod(-7,4) a = 1 a = rem(-7,4) a = -3

  46. Extracting Triangular and Diagonal Parts I • B = tril (A) bij=aij if i j, zero otherwise. B = triu (A) bij=aij if i j, zero otherwise. B = tril (A,k) bij=aij if i+k j, zero otherwise. B = triu (A,k) bij=aij if i+k j, zero otherwise. • diag(v) puts v on the main diagonal. v=diag(A) return a vector v with A’s diagonal.

  47. Extracting Triangular and Diagonal Parts II • T = toeplitz(c,r) is equivalent to • T = zeros(n); for k = 1:n j = n-k; if k<n T = T + diag( r(k+1)*ones(j,1) , k); end T = T + diag( c(k)*ones(j+1,1) , -k+1); end

  48. Extracting Triangular and Diagonal Parts III • Take c=[6 7 8 9 10]; r=[1 2 3 4 5]; n=5; into above two function. We can show T =[ 6 2 3 4 5 7 6 2 3 4 8 7 6 2 3 9 8 7 6 2 10 9 8 7 6]

  49. ‘rank’ • If A is a m-by-n matrix, rank(A)= min{m,n}. • It Is also possible to base the rank decision on an arbitrary tolerance.If tol is a nonnegative scalar then r = rank(A , tol) • A = [ 1 2 ; 3 4 ; 5 6 ] rank(A)=min{3,2}=2 • A = [1 0.1 ; 3 0.2 ; 5 0.3 ] rank(A,1)=1

  50. Condition • cond(A) can be computed the 2-norm condition (the ratio of the largest singular value of X to the smallest) of a matrix A.

More Related