PHYS108 Matrices, Vectors, - PowerPoint PPT Presentation

1 / 24
About This Presentation
Title:

PHYS108 Matrices, Vectors,

Description:

Be able to identify important built-in functions required to create and ... columnwise from A. 14. Additional functions to generate matrices: magic(n) pascal(n) ... – PowerPoint PPT presentation

Number of Views:27
Avg rating:3.0/5.0
Slides: 25
Provided by: admi1432
Category:

less

Transcript and Presenter's Notes

Title: PHYS108 Matrices, Vectors,


1
PHYS108 Matrices, Vectors, Scalars 09/09/09
2
  • Understand essentials of operator precedence.
  • Be able to use square brackets, to create
    matrices.
  • Be able to identify important built-in functions
    required to create and manipulate matrices.

3
  • To save command window input/output
  • gtgt diary(lecture_09_09_09)

4
  • 2.1 Scalar Arithmetic Operations
  •  Scalars
  • add (), subtract(-), multiply (), divide (/),
    exponentiation ().
  • BUT multiple operations 533? gt 153 or
    527 ?
  • Precedence determines order of arithmetic
    operations. Rules are ordered from highest(1) to
    lowest precedence level (4)
  •  
  • Parentheses ().
  • Exponentiation ().
  • Multiplication () and division (/).
  • Addition () and subtraction (-).
  • Within each level, operators with equal
    precedence are evaluated in order from left to
    right.
  • Such rules of precedence exist for ALL systems
    of computer arithmetic

5
  • gtgt 24/10 1st then
  • ans 1.6000
  • gtgt 5 49 1st then
  • ans 41
  • gtgt -9 -822 1st then then -
  • ans -41
  • gtgt 6 6/23 / then then
  • ans 15
  • gtgt 6 6/(23) 1st inside () then / then
  • ans 7
  • gt 224 2(24) 44 216
  • ans 256 65536

6
  • Common Intrinsic MATH Functions
  • Math Notation MATLAB name
  • sinx sin(x)
  • cosx cos(x)
  • tanx tan(x)
  • a abs(a)
  • lnex log(x)
  • log10x log10(x)
  • vx sqrt(x)
  • See
  • fx gt MATLAB gt Mathematics gt Elementary
    Mathematics

7
  • 2.2 Creation of Matrices and Vectors
  • Explicitly via square brackets,
  • row vectors separate elements by spaces or
    commas
  • gtgt v 3 2 1
  • v 3 2 1
  • column vectors separate elements by semicolons
  • gtgt w 6 5 4
  • w 6
  • 5
  • 4

8
  • Matrix gt rectangular table of numbers
  • Ar,c gt r row index (start from 1)
    from top to bottom
  • c column index (start from 1) from left
    to right

9
  • Separate rows by semicolons
  • gtgt A 1 2 3 4 5 6 7 8 9 10 11 12
  • A 1 2 3 4
  • 5 6 7 8
  • 9 10 11 12
  • Create matrices from predefined vectors and
    matrices
  • gtgt u vvv
  • u 3 2 1
  • 3 2 1
  • 3 2 1

10
  • gtgt t vvv w
  • t 3 2 1 6
  • 3 2 1 5
  • 3 2 1 4
  • gtgt A t
  • ans 1 2 3 4 3 2 1 6
  • 5 6 7 8 3 2 1 5
  • 9 10 11 12 3 2 1 4
  • gtgt A t
  • ans 1 2 3 4
  • 5 6 7 8
  • 9 10 11 12
  • 3 2 1 6
  • 3 2 1 5
  • 3 2 1 4

11
  • Colon operator
  • lower-value step upper-value
  • step can be positive or negative
  • gtgt x 00.251
  • x 0 0.2500 0.5000 0.7500 1.0000
  • gtgt y 1.0-0.250.0
  • y 1.0000 0.7500 0.5000 0.2500 0

12
  • SOME Intrinsic Functions
  • transpose(x) transpose of column vector is a
    row vector and transpose of a row vector is a
    column vector.
  • gtgt transpose(w)
  • ans 6 5 4
  • gtgt transpose(v)
  • ans 3
  • 2
  • 1
  • Transpose of matrix switches row column indices
  • Transpose(Aij) Aji

13
  • gtgt A 1 2 3 4 5 6
  • A 1 2 3
  • 4 5 6
  • gtgt transpose(A)
  • ans 1 4
  • 2 5
  • 3 6
  • linspace(x1, x2, N) generates a row vectors with
    N equal spaced points between x1 and x2.

14
  • gtgt linspace(0.0, .01, 5)
    gt five-member row
  • ans 0 0.2500 0.5000 0.7500 1.0000
  • gtgt transpose(linspace(0.0, 1.0, 5)) gt
    five-member column
  • zeros(m,n) gt m-by-n matrix of 0s.
  • ones(n) gt n-by-n matrix of 1s.
  • eye(n) gt n-by-n identity matrix.
  • rand(n) gt n-by-n matrix of
    uniformly distributed random real numbers.
  • randi(imax, m,n) gt returns an m-by-n matrix
    of random integers uniformly between 1 and
    imax.
  • reshape(A, m, n) gt returns m-by-n matrix
    taken
  • columnwise from A.

15
  • Additional functions to generate matrices
  • magic(n)
  • pascal(n)
  • hilb(n)
  • See gallery for 50 matrices with unusual
    properties
  • fx gallery

16
  • gtgt zeros(3,2)
  • ans 0 0
  • 0 0
  • 0 0
  • gtgt ones(2,3)
  • ans 1 1 1
  • 1 1 1
  • gtgt rand(3,4)
  • ans
  • 9.7540e-002 9.5751e-001 9.7059e-001
    8.0028e-001
  • 2.7850e-001 9.6489e-001 9.5717e-001
    1.4189e-001
  • 5.4688e-001 1.5761e-001 4.8538e-001
    4.2176e-001
  • gtgt randi(1000, 4,3)
  • ans 916 36 758
  • 793 850 744
  • 960 934 393

17
gtgt x linspace(1, 24, 24) gtgt A reshape(x, 6,
4) A 1 7 13 19 2 8
14 20 3 9 15 21 4 10
16 22 5 11 17 23 6 12
18 24 gtgt A reshape(x, 4, 6) A 1 5
9 13 17 21 2 6 10 14
18 22 3 7 11 15 19 23
4 8 12 16 20 24 gtgt B
transpose(A) B 1 2 3 4 5
6 7 8 9 10 11 12 13
14 15 16 17 18 19 20 21
22 23 24
18
  • Functions to Manipulate Vectors Matrices
  • Vector
  • length(x) returns the number of elements of
    vector x.
  • mean(x) returns the mean value of the
    elements of x.
  • median(x) returns the median value of the
    elements of x.
  • sort(x) sorts x in ascending order
  • sort in descending order, sort(x,
    descend)
  • sum(x) sum of the elements of x.
  • min(x) returns the smallest element of x.
  • Essential property of ALL such functions, applied
    to matrices, is that they treat each column of a
    matrix as a distinct vector.
  •  

19
  • gtgt A 1 2 3 4 5 6 7 8 9
  • gtgt sort(A, 'descend')
  • ans 7 8 9
  • 4 5 6
  • 1 2 3
  • gtgt sum(A)
  • ans 12 15 18
  • gtgt min(A)
  • ans 1 2 3

20
  • If you want the relevant function applied to
    entire matrix, apply the function to A() -
  • gtgt sum(A()) MATLAB TRICK
  • ans 45
  • gtgt max(A())
  • ans 9
  • gtgt sort(A(), 'descend')
  • ans 9
  • 8
  • 7
  • 6
  • 5
  • 4
  • 3
  • 2
  • 1
  • Sometimes one sees reference to A() where A is
    known to be a matrix. This operation transforms a
    two-dimensional matrix, A, into a corresponding
    one-dimensional vector, constructed in column
    major order
  •  

21
  • Extract/Identify Elements via Subscripts and
    Colon Operators
  • The relevant subscript/index is identified by an
    integer placed inside a pair of matched
    parentheses.
  • gtgt y 026
  • gtgt y(2)
  • ans 2
  • gtgt y(23)
  • ans 2 4
  • gtgt y(4 2 1)
  • ans 6 2 0 

22
  • gtgt D 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
  • D 1 2 3 4 5
  • 6 7 8 9 10
  • 11 12 13 14 15
  • gtgt D(1,) identify 1st row
  • ans 1 2 3 4 5
  • gtgt D(, 5) identify 5th column
  • ans 5
  • 10
  • 15
  • gtgt D(23, 35)
  • ans 8 9 10
  • 13 14 15
  • gtgt D(23, 35) -1 -2 -3 -4 -5 -6 Replace
    section
  • D 1 2 3 4 5
  • 6 7 -1 -2 -3
  • 11 12 -4 -5 -6

23
  • Empty 0x0 matrix
  •  
  • The operator can be employed to remove
    columns/rows from matrices
  •  
  • gtgt C 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
    16
  • gtgt C(2,) removes 2nd row
  • C 1 2 3 4
  • 9 10 11 12
  • 13 14 15 16
  • gtgt C(,1) removes 1st
    column
  • C 2 3 4
  • 10 11 12
  • 14 15 16
  •  
  •  

24
  • Understand essentials of operator precedence gt
    () trumps everything else .
  • Be able to use square brackets, , to create
    small vectors and matrices.
  • Be able to identify some important built-in
    functions linspace, transpose, rand, randi,
    randn, min, max, size, sort, ones, zeros, eye
  • Be familiar with colon operator.
Write a Comment
User Comments (0)
About PowerShow.com