Review - PowerPoint PPT Presentation

1 / 27
About This Presentation
Title:

Review

Description:

q(i, j) = entry in row i, column j of matrix q. ... If there's a figure named '1', brings it to front. If not, creates one and brings it to front ... – PowerPoint PPT presentation

Number of Views:37
Avg rating:3.0/5.0
Slides: 28
Provided by: JFH
Category:
Tags: review

less

Transcript and Presenter's Notes

Title: Review


1
Lecture 2
2
Review
  • Matrix construction the concatenation operator
  • 1 2 3 gt 1 2 3 and 1,2,3 gt 1 2 3
  • 123 gt
  • 1
  • 2
  • 3
  • Mix-and-match is OK 1 2 3 4 gt
  • 1 2
  • 3 4

3
The colon operator
  • 15 gt 1 2 3 4 5
  • 125 gt 1 3 5
  • 5 -1 1 gt 5 4 3 2 1

4
Matrix indexing
  • q(i, j) gt entry in row i, column j of matrix q.
  • q(1 2, 3) gt entries in rows 1 and 2, column 3.
  • Logical indexing If q 13, then q(false
    true true)gt 2 3
  • end can be used in indexing, too
  • q(1end) gt 1 2 3
  • q(1end-1) gt 1 2

5
Quiz
  • If we type
  • gt m round(5rand(1,40)), we get
  • m 4 4 3 2 2 and so on, a 1
    x 40 array of small numbers
  • Write an expression (using m) to produce a list
    of 0s and 1s like
  • 1 0 0 1
  • where a 1 means that m(i) is the same as
    m(i1).
  • Hint a b tells (0/1) whether a and b are
    equal.

6
Types
  • Everything in matlab has a TYPE you can see it
    in the upper left corner, when you click on
    workspace
  • Integers whole numbers like 1, 27, -982
  • double double-precision floating-point numbers
    (i.e., things like 7.488, or 3.1415)
  • boolean true or false (stored as 1 or 0)

7
Types (II)
  • character a, t, W, /, etc. stored as
    small integers (0 to 255, more or less a 32).
  • uint8 unsigned 8-bit integer. A number thats
    stored using 8 bits (one byte of memory), and can
    range from 0 to 255. These come up a lot in
    images.

8
Types(III)
  • Compound types MATRICES
  • Matrices have elements, all of the same type. So
    1 2 3 is a matrix of integers, while a b
    c is a matrix of characters
  • 1 x n character matrices are called strings,
    and they print out oddly
  • gta b c
  • ans abc

9
Types (IV)
  • Shorthand for creating character arrays
  • abc is the same as a b c

10
Types (V)
  • Complex numbers i and j both denote sqrt(-1).
  • You can write u 3 4i

11
Brief digression on complex arithm.
  • 3 2i
  • 4 3i
  • ----------
  • 7 i

12
More complex arithmetic
  • 3 2i
  • 4 3i
  • ----------
  • (-3i)(2i) (-3i)3 4 (2i) (43)
  • -6i2 -9i 8i 12
  • -6(-1) -i 12
  • 6 -i 12
  • 18-i

13
One last bit of complex arith
  • For a complex number z a bi, the length or
    modulus is defined to be
  • z sqrt( a2 b2 )
  • So 1 2i sqrt(12 22) sqrt(5).

14
Geometry and complex nums
  • We plot complex numbers on the plane by plotting
    a bi at the point (a,b)

1i
20.5i
15
Matrix indexing
  • Give subscripts, as before.
  • Subscripts can be matrices, too.

16
Matrix indexing
  • Matlab has another way to refer to elements, with
    just one index. In the 2 x 3 matrix m
  • 1 8 5
  • 4 0 9
  • the indices are
  • 1 3 5
  • 2 4 6
  • so that m(1) 1, and m(3) 8, and m(6) 9.
  • Weird, but amazingly useful.

17
Some useful procedures and idioms
  • rand(n) an n x n matrix of random numbers
    between 0 and 1
  • ones(n) and n x n matrix of ones.
  • zeros(n) zeros
  • rand(n, k) an n x k matrix of randoms
  • ones(n, k) and n x k matrix of ones, etc.
  • Idiom many procs have two forms like this.

18
Multi-dimensional arrays
  • You can let m ones(3,4,5) to get a 3x4x5 array.
  • We wont use this for a while yet.

19
Idioms
  • m rand(3,5)
  • u size(m)
  • ans 3 5
  • u(1)
  • ans 3
  • Simpler
  • u size(m, 1)
  • ans 3

20
Operations on types
  • You can check whether something is a logical, or
    a float, or an int or a uint8, with builtin
    procedures like
  • isint(), isfloat(), isuint8()
  • Rarely needed.
  • You can convert, too double(X), round(Y),
    uint8(Z)

21
Figures
  • figure(1) does two things
  • If theres a figure named 1, brings it to front
  • If not, creates one and brings it to front
  • A figure is a window in which to draw stuff.
  • Figures all get numbers (1, 2, )
  • gcf (get current figure) gets the number of the
    currently active figure.

22
Figures(II)
  • figure(gcf) gt creates a figure if there is none
    brings current figure forward to be visible.
  • Possibly the most-used line of matlab.

23
Some examples
24
Revised model for matlab
a 4
b 4 5 6
z 1 2i
25
Operations
  • Assigning a value to a variable (b 2)
  • Write down the name and value on current sheet of
    paper
  • c 3 5 b
  • Grab a postit, do computations on it,
  • Copy answer to pad, next to c
  • When you need the value for b, look on the pad

26
Using a procedure
  • q 3
  • b increment(q)
  • Evaluate the argument of the procedure (in this
    case, look up q.
  • Tear off a new sheet of paper, covering the old
    one write down the value of the argument next to
    the name of the argument
  • Do all the procedures work.
  • Write the return value on a postit.
  • Throw away this worksheet
  • Copy postit value to b.

27
Example procedure
  • function y increment(x)
  • y x1
  • In this case, weve evaluated the argument q
    (its 3) we write, on NEW sheet
  • x 3
  • We do the procedure steps assign y 31, so y
    4.
  • We copy this value onto a postitthrow away the
    worksheet
  • and assign the post-it value b.
  • At this point, x has no meaning!
Write a Comment
User Comments (0)
About PowerShow.com