1 / 64

DREAM

DREAM. IDEA. PLAN. IMPLEMENTATION. Mat rix Lab oratory. Introduction to Matlab By Dr. Kourosh Kiani. Course Structure. Spread over 8 weeks 1 classes per week 2 hour per class. Basic Operations. Scalars, Vectors, Matrices. MATLAB treat variables as “matrices”

Download Presentation

DREAM

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. DREAM IDEA PLAN IMPLEMENTATION

  2. MatrixLaboratory Introduction to MatlabByDr. Kourosh Kiani

  3. Course Structure • Spread over 8 weeks • 1 classes per week • 2 hour per class

  4. Basic Operations

  5. Scalars, Vectors, Matrices • MATLAB treat variables as “matrices” • Matrix (m  n) - a set of numbers arranged in rows (m) and columns (n) • Scalar: 1  1 matrix • Row Vector: 1  n matrix • Column Vector: m  1 matrix

  6. >> a=[1 2 3; 4 5 6] a = 1 2 3 4 5 6 >> size(a) ans = 2 3 >> pi ans = 3.1416 >> size(pi) ans = 1 1

  7. variable name = [#, #, # ;#, #, # ;#, #, # ;…..] 1st row 2nd row 3rd row • matrix = [ 1 2 3; 4 5 6 ] matrix = 1 2 3 4 5 6 • matrix = [ 1 2 3; 4 5 ] ??? Error • a = [ 5 (2+4) ] a = 5 6

  8. Vectors and Matrices • Vectors (arrays) are defined as • >> v = [1, 2, 4, 5] • >> w = [1; 2; 4; 5] • Matrices (2D arrays) defined similarly • >> A = [1,2,3;4,-5,6;5,-6,7]

  9. Matrices Building matrices with [ ]: A = [2 7 4] A = [2; 7; 4] A = [2 7 4; 3 8 9] B = [ A A ] 2 7 4 2 7 4 2 7 4 3 8 9 ?

  10. Matrices Building matrices with [ ]: A = [2 7 4] A = [2; 7; 4] A = [2 7 4; 3 8 9] B = [ A A ] 2 7 4 2 7 4 2 7 4 3 8 9 2 7 4 2 7 4 3 8 9 3 8 9

  11. Matrices • transpose operator  ' • u = [ 1:3 ]' u = 1 2 3 • v = [ u u ] v = 1 1 2 2 3 3

  12. Fetching Elements How would you address to number 0? Row first, column next; >> A(2,4) How about 2? >> A(3,2)

  13. Fetching Elements • Array indices start from 1 • x = [ -2 0 9 1 4 ]; • x(2) ans = 0 • x(4) ans = 1 • x(8) ??? Error • x(-1) ??? Error

  14. MATLAB BASICS Variables and Arrays • Array:A collection of data values organized into rows and columns, and known by a single name. Row 1 Row 2 Row 3 arr(3,2) Row 4 Col 1 Col 2 Col 3 Col 4 Col 5

  15. 4 columns 3 2 4 -10 1 15 -2 9 -4 6 3 7 X = 3 rows Access Matrix Elements Note: Indices start by 1not by 0 X(1,:) = Index exceeds matrix dimensions. X(2,3) = X(4,1) = X(3,4) =

  16. Fetching Elements How can we extract the collection of numbers in the dotted box? That is, the numbers in the 1st through 3rd rows, 2nd through 4th columns… Specify the row and column numbers by counting them… A(1:3, 2:4)

  17. Indexing Matrices • Indexing using parentheses • >> A(2,3) • Index submatrices using vectorsof row and column indices • >> A([2 3],[1 2]) • Ordering of indices is important! • >> B=A([3 2],[2 1]) • >> B=[A(3,2),A(3,1);A(2,2);A(2,1)]

  18. Indexing Matrices • Index complete row or column using the colon operator • >> A(1,:) • Can also add limit index range • >> A(1:2,:) • >> A([1 2],:) • General notation for colon operator • >> v=1:5 • >> w=1:2:5

  19. Submatrices A matrix can be indexed using another matrix, to produce a subset of its elements: a = [100 200 300 400 500 600 700] b = [3 5 6] c = a(b): 300 500 600

  20. Array Subscripting / Indexing 1 2 3 4 5 A = 1 6 11 16 21 2 7 12 17 22 3 8 13 18 23 4 9 14 19 24 5 10 15 20 25 4 10 1 6 2 1 2 3 4 5 8 1.2 9 4 25 A(1:5,5) A(:,5) A(21:25) A(1:end,end) A(:,end) A(21:end)’ 7.2 5 7 1 11 0 0.5 4 5 56 A(3,1) A(3) 23 83 13 0 10 A(4:5,2:3) A([9 14;10 15]) • Use () parentheses to specify index • colon operator (:) specifies range / ALL • [ ] to create matrix of index subscripts • ‘end’ specifies maximum index value

  21. Skipping rows/columns using colon increment • w=v(1:2:5,3) picks rows 1,3,5 and column 3

  22. Matrix Manipulations • Concatenation • Join small matrices to make bigger one A = [1 2]; B = [A’ [3 ; 4]]; % ’ is the transpose operator B = • Deletion • A(1) = [ ]; %remove one element from array • B(1,:) = [ ]; %remove row from matrix

  23. Colon Operator • Creating new matrices from an existing matrix C = [1,2,5; -1,0,1; 3,2,-1; 0,1,4] F = C(:, 2:3) = [2,5; 0,1; 2,-1; 1,4]

  24. Colon Operator • Creating new matrices from an existing matrix C = [1,2,5; -1,0,1; 3,2,-1; 0,1,4] E = C(2:3,:) = [-1 0 1; 3 2 -1]

  25. Colon Operator • Creating new matrices from an existing matrix C = [1,2,5; -1,0,1; 3,2,-1; 0,1,4] G = C(3:4,1:2) = [3,2; 0,1]

  26. More on the colon operator • For M=[1,2,3,4,5;2,3,4,5,6;3,4,5,6,7] • x = M(:,1) is the column vector, the 1st column of M • x = M(:,3) is a column vector, the 3rd column of M • y = M(2,:) is a row vector, the second row of M • w = M(2:3,:) is a 2 rows and 5 columns matrix made up of the last two rows of M • w = M(2:3,4:5) is a 2 rows and 2 columns matrix made up the elements, [5,6;6,7] • And don’t forget, z = M(2,4), then z = 5

  27. Review • Tell me how to index (with subscripts) the first three elements of this array: >> x(1,1:3) >> x(1:3)

  28. Review • Tell me how to index (with subscripts) the indicated elements elements of this array: >> x([2 4])

  29. Review • Suppose we want to reorder the items in x, how do we write the subscripts? >> x([5 3 2 4 1])

  30. Randomization • Suppose we want to randomize the items in x • Step 1: create a random array >>randperm(5) • >> randperm(5) • ans = • 5 1 4 3 2 >>randperm(length(x))

  31. Randomization • Suppose we want to randomize the items in x • Step 2: use the random array to index x >> x(randperm(5)) >>y = randperm(5) >>x(y)

  32. 7 4 5 6 N-D Arrays Arrays with more than 2-dimensions are referred to as • “N-D arrays”, or • “multidimensional arrays” Suppose we pictorially denote a 4-by-7 array as Then a 4-by-7-by-6-by-5 array would be depicted as

  33. 1 2 3 4 9 10 11 12 5 6 7 8 Array Indexing Order (2-D) 1st element 2nd element 3rd 4th 5th 6th 7th 8th 9th 10th 11th 12th Consider a 4-by-3 double array, say A = reshape(1:12,[4 3]) How is it arranged in memory? A(1,1) A(2,1) A(3,1) A(4,1) A(1,2) A(2,2) A(3,2) A(4,2) A(1,3) A(2,3) A(3,3) A(4,3) full index linear index For 2-d double array, to move through memory sequentially • the first index changes the fastest, and • the second index changes the slowest Same pattern holds for N-D double arrays

  34. 1 2 3 4 13 14 15 16 1 2 3 4 9 10 11 12 21 22 23 24 9 10 11 12 5 6 7 8 17 18 19 20 5 6 7 8 Linear Indexing (1,1) 1st (2,1) 2nd (3,1) 3rd (4,1) 4th (1,2) 5th (2,2) 6th (3,2) 7th (4,2) 8th (1,3) 9th (2,3) 10th (3,3) 11th (4,3) 12th (1,1,1) 1st (2,1,1) 2nd (3,1,1) 3rd (4,1,1) 4th (1,2,1) 5th (2,2,1) 6th (3,2,1) 7th (4,2,1) 8th (1,3,1) 9th (2,3,1) 10th (3,3,1) 11th (4,3,1) 12th (1,1,2) 13th (2,1,2) 14th (3,1,2) 15th (4,1,2) 16th (1,2,2) 17th (2,2,2) 18th (3,2,2) 19th (4,2,2) 20th (1,3,2) 21st (2,3,2) 22nd (3,3,2) 23rd (4,3,2) 24th 4-by-3 example 4-by-3-by-2 example

  35. Initialization using shortcut statements • colon operator  first:increment:last • x = 1:2:10 x = 1 3 5 7 9 • y = 0:0.1:0.5 y = 0 0.1 0.2 0.3 0.4 0.5

  36. Colon Operator • Variable_name = a:step:b time = 0.0:0.5:2.5 time = [0.0, 0.5, 1.0, 1.5, 2.0, 2.5] • Negative increment values = 10:-1:2 values = [10, 9, 8, 7, 6, 5, 4, 3, 2]

  37. More array manipulation examples • v=[1:5] is equivalent to v=[1 2 3 4 5]. • v=[1:2:10] is equivalent to v=[1 3 5 7 9] • v=[5:-1:1] is equivalent to v=[5 4 3 2 1] • Guess what v=[1:3;4:6;7:10] is equivalent to?

  38. Matrix Concatenation

  39. linspace Function • linspace(x1, x2) gives 100 evenly spaced values between x1 and x2 x = linspace(x1,x2) • linspace(a,b,n) generate n equally spaced points between a and b x = linspace(a,b,n) » linspace(0,2,11) ans = Columns 1 through 7 0 0.2000 0.4000 0.6000 0.8000 1.0000 1.2000 Columns 8 through 11 1.4000 1.6000 1.8000 2.0000

  40. logspace Function • logspace(a,b,n) generates a logarithmically equally spaced row vector • x = logspace(a,b,n) • logspace(a,b) generates 50 logarithmically equally spaced points • x = logspace(a,b) » logspace(-4,2,7) ans = 0.0001 0.0010 0.0100 0.1000 1.0000 10.0000 100.0000

  41. Special Matrices

  42. Special Matrices rand: uniformly distributed random numbers. A = rand(3,5) A = 0.9501 0.4860 0.4565 0.4447 0.9218 0.2311 0.8913 0.0185 0.6154 0.7382 0.6068 0.7621 0.8214 0.7919 0.1763 randn: normally distributed random numbers. B = randn(3,5) B = -1.1465 -0.0376 -0.1867 2.1832 1.0668 1.1909 0.3273 0.7258 -0.1364 0.0593 1.1892 0.1746 -0.5883 0.1139 -0.0956

  43. Scalar Arithmetic Operations • In order of priority (Matrix inverse) Example: x = (a + b*c)/d^2 count = count + 1

  44. Order of Precedence of Arithmetic Operations • Parentheses, starting with the innermost pair • Exponentiation, from left to right • Multiplication and division with equal precedence, from left to right • Addition and subtraction with equal precedence, from left to right Examples: factor = 1 + b/v + c/v^2 slope = (y2 - y1)/(x2 - x1) loss = f * length/dia * (1/2 * rho * v^2) func = 1 + 0.5*(3*x^4 + (x + 2/x)^2)

  45. Order of Precedence of Arithmetic Operations • The priority order can be overridden with parentheses » y = -7.3^2 y = -53.2900 » y=(-7.3)^2 y = 53.2900 » a=3; b=5; c=2; » s1 = a-b*c s1 = -7 » s2=(a-b)*c s2 = -4 Exponentiation has higher priority than negation Multiplication has higher priority than subtraction

  46. Array Operations • An array operation is performed element-by-element MATLAB: C = A.*B;

  47. Element-by-Element Operations

  48. Vector and Matrix operations But a*b gives an error (undefined) because dimensions are incorrect. Need to use .*

  49. Matrix vs Element Math Element Math Matrix Math

  50. Vectorized Matrix Operations

More Related