Lecture 4 Sept 8 - PowerPoint PPT Presentation

About This Presentation
Title:

Lecture 4 Sept 8

Description:

Lecture 4 Sept 8 Complete Chapter 3 exercises Chapter 4 – PowerPoint PPT presentation

Number of Views:70
Avg rating:3.0/5.0
Slides: 40
Provided by: Engine91
Category:

less

Transcript and Presenter's Notes

Title: Lecture 4 Sept 8


1
  • Lecture 4
    Sept 8
  • Complete Chapter 3 exercises
  • Chapter 4

2
Exercise 3.1 sum(2.(0 4)) Exercise
3.5
3
(No Transcript)
4
(No Transcript)
5
Exercise 3.8. Write the MATLAB version of the
boolean statement a lt b lt c. Answer
Exercise 3.12 How many leap years are between
1780 and 2832, inclusive? Recall the definition
of a leap year.
6
Solution to Exercise 3.12
7
Chapter 4 arrays, collections and indexing
  • This chapter discusses the basic calculations
    involving rectangular collections of numbers in
    the form of vectors and arrays. For each of these
    collections, you will learn how to
  • Create them
  • Manipulate them
  • Access their elements
  • Perform mathematical and logical operations on
    them

8
Concept Data Collections
  • This section considers two very common ways to
    group data in arrays and in vectors.
  • Data Abstraction allows us to refer to groups of
    data collectively
  • all the temperature readings for May or
  • all the purchases from Wal-Mart.
  • We can not only move these items around as a
    group, but also perform mathematical or logical
    operations on these groups, e.g.
  • compute the average, maximum, or minimum
    temperatures for a month
  • A Homogeneous Collection is constrained to accept
    only items of the same data type in this case,
    they will all be numbers

9
MATLAB Vectors
  • Individual items in a vector are usually referred
    to as its elements. Vector elements have two
    separate and distinct attributes that make them
    unique in a specific vector
  • their numerical value and
  • their position in that vector.
  • For example, the individual number 66 is the
    third element in this vector. Its value is 66 and
    its index is 3. There may be other items in the
    vector with the value of 66, but no other item
    will be located in this vector at position 3.

10
Vector Manipulation
  • We consider the following basic operations on
    vectors
  • Creating a Vector
  • Determining the size of a Vector
  • Extracting data from a vector by indexing
  • Shortening or expanding a Vector
  • Mathematical and logical operations on Vectors

11
Creating a Vector
  • Entering the values directly, e.g.
  • A 2, 5, 7, 1, 3
  • Entering the values as a range of numbers e.g.,
  • B 1320
  • Using the linspace(...) function e.g.
  • c linspace (0, 20, 11)
  • Using the functions zeros(1,n), ones(1,n),
    rand(1,n) and randn(1,n) to create vectors
    filled with 0, 1, or random values between 0 and
    1

12
Exercise Write a code segment in Matlab to
generate a sequence containing the outcomes of 50
independent rolls of a fair die. Thus we want to
generate a vector of size(100) where each element
of the vector is a random member of the set 1,
2, 3, 4, 5, 6. Note that rand() gives a random
real number between 0 and 1.
13
Later we will write code to count how many times
the outcome was j for different values of j 1,
2, , 6. If the random number generator is a good
one, we expect all these numbers to be close to
each other.
14
Size of vectors and arrays
  • MATLAB provides two functions to determine the
    size of arrays in general (a vector is an array
    with one row)
  • the function size(A) when applied to the array A
    returns vector containing two quantities the
    number of rows and the number of columns
  • The function length(A) returns the maximum value
    in the size of an array for a vector, this is
    its length.

15
Indexing a Vector
  • The process of extracting values from a vector,
    or inserting values into a vector
  • Syntax
  • v(index) returns the element(s) at the
    location(s) specified by the vector index.
  • v(index) value replaces the elements at the
    location(s) specified by the vector index.
  • The indexing vector may contain either numerical
    or logical values

16
(No Transcript)
17
(No Transcript)
18
(No Transcript)
19
Exercise Write a one-line statement to compute
the average of a set of numbers stored in vector
x.
20
Exercise Write a one-line statement to compute
the average of a set of numbers stored in vector
x. Answer gtgt sum(x) / length(x)
21
Numerical Indexing
  • The indexing vector may be of any length
  • It should contain integer (non-fractional)
    numbers
  • The values in the indexing vector are constrained
    by the following rules
  • For reading elements, all index values j must be
  • 1 lt j lt length (vector)

22
Replacement Rules
  • Either
  • All dimensions of the blocks on either side of
    the replacement instruction must be equal, or
  • There must be a single element on the RHS of the
    replacement
  • If you replace beyond the end of the existing
    vector, the vector length is automatically
    increased.
  • Any element not specifically replaced remains
    unchanged.
  • Elements beyond the existing length not replaced
    are set to 0.

23
(No Transcript)
24
Logical Indexing
  • The indexing vector length must be less than or
    equal to the original vector length
  • It must contain logical values (true or false)
  • Access to the vector elements is by their
    relative position in the logical vector
  • When reading elements, only the elements
    corresponding to true index values are returned
  • When replacing elements, the elements
    corresponding to true index values are replaced
  • Beware logical vectors in Matlab echo in the
    Command window as 1 or 0, but they are not the
    same thing.

25
(No Transcript)
26
Exercise Let u be a vector. Write a Matlab code
segment that determines the number of occurrences
of 7 in it. Example gtgt u 1 3 5 7 9 7 5 3
1 gtgt lt your code heregt gtgt ans 2
27
Exercise Let u be a vector. Write a Matlab code
segment that determines the number of occurrences
of 7 in it. Example gtgt u 1 3 5 7 9 7 5 3
1 gtgt lt your code heregt gtgt ans
2 Answer sum(u 7)
28
Find function
29
Exercise Write a program in Matlab to split a
vector u into two vectors v and w where v
contains all the numbers up to the largest number
and w contains the rest. Example gtgt u 1 8 3
12 6 9 11 gtgt lt your code heregt gtgt v ans 1
8 3 12 gtgt w ans 6 9 11
30
Exercise Write a program in Matlab to split a
vector u into two vectors v and w where v
contains all the numbers up to the largest number
and w contains the rest. Example gtgt u 1 8 3
12 6 9 11 gtgt v u(1find(u max(u))) gtgt v ans
1 8 3 12
31
Exercise Write a Matlab expression to determine
if there are any negative numbers in a vector
u. Answer
32
Exercise Write a Matlab expression to determine
if there are any negative numbers in a vector
u. Answer sum(u lt 0) gt 0
33
Operating on Vectors
  • Three techniques extend directly from operations
    on scalar values
  • Arithmetic operations
  • Logical operations
  • Applying library functions
  • Two techniques are unique to arrays in general,
    and to vectors in particular
  • Concatenation
  • Slicing (generalized indexing)

34
Arithmetic operations
  • Arithmetic operations
  • gtgt A 2 5 7 1 3
  • gtgt A 5
  • ans
  • 7 10 12 6 8
  • gtgt A . 2
  • ans
  • 4 10 14 2 6
  • gtgt B -113
  • B
  • -1 0 1 2 3

35
Arithmetic operations (continued)
  • gtgt A . B element-by-element multiplication
  • ans
  • -2 0 7 2 9
  • gtgt A B matrix multiplication!!
  • ??? Error using gt mtimes
  • Inner matrix dimensions must agree.
  • gtgt C 1 2 3
  • C
  • 1 2 3
  • gtgt A . C A and C must have the same length
  • ??? Error using gt times
  • Matrix dimensions must agree.

36
Logical operations
  • gtgt A 2 5 7 1 3
  • gtgt B 0 6 5 3 2
  • gtgt A gt 5
  • ans
  • 0 1 1 0 0
  • gtgt A gt B
  • ans
  • 1 0 1 0 1
  • gtgt C 1 2 3
  • gtgt A gt C
  • ??? Error using gt gt
  • Matrix dimensions must agree.

37
Logical operations (continued)
  • gtgt A true true false false
  • gtgt B true false true false
  • gtgt A B
  • ans
  • 1 0 0 0
  • gtgt A B
  • ans
  • 1 1 1 0
  • gtgt C 1 0 0 NOT a logical vector
  • gtgt A(C) yes, you can index logical vectors, but
    ...
  • ??? Subscript indices must either be real
    positive integers or logical.

38
Applying library functions
  • All MATLAB functions accept vectors of numbers
    rather than single values and return a vector of
    the same length.
  • Special Functions
  • sum(v) and mean(v) consume a vector and return
    a number
  • min(v) and max(v) return two quantities the
    minimum or maximum value in a vector, plus the
    position in that vector where that value
    occurred.

39
Applying library functions
  • round(v), ceil(v), floor(v), and fix(v) remove
    the fractional part of the numbers in a vector by
    conventional rounding, rounding up, rounding
    down, and rounding toward zero, respectively.
  • We can also apply functions like sqrt or sine or
    cosine to each element of a vector.
Write a Comment
User Comments (0)
About PowerShow.com