lecture 4 sept 8

39
Lecture 4 Sept 8 Complete Chapter 3 exercises Chapter 4

Upload: zinna

Post on 19-Jan-2016

49 views

Category:

Documents


0 download

DESCRIPTION

Lecture 4 Sept 8 Complete Chapter 3 exercises Chapter 4. Exercise 3.1: sum(2.^(0 : 4)) Exercise 3.5:. Exercise 3.8. Write the MATLAB version of the boolean statement a

TRANSCRIPT

Page 1: Lecture 4                                                                           Sept 8

Lecture 4

Sept 8

•Complete Chapter 3 exercises

• Chapter 4

Page 2: Lecture 4                                                                           Sept 8

Exercise 3.1:

sum(2.^(0 : 4))

Exercise 3.5:

Page 3: Lecture 4                                                                           Sept 8
Page 4: Lecture 4                                                                           Sept 8
Page 5: Lecture 4                                                                           Sept 8

Exercise 3.12: How many leap years are between 1780 and 2832, inclusive? Recall the definition of a leap year.

Exercise 3.8. Write the MATLAB version of the boolean statement a <= b <= c.

Answer:

Page 6: Lecture 4                                                                           Sept 8

Solution to Exercise 3.12:

Page 7: Lecture 4                                                                           Sept 8

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

Page 8: Lecture 4                                                                           Sept 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

Page 9: Lecture 4                                                                           Sept 8

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.

Page 10: Lecture 4                                                                           Sept 8

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

Page 11: Lecture 4                                                                           Sept 8

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 = 1:3:20

• 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

Page 12: Lecture 4                                                                           Sept 8

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.

Page 13: Lecture 4                                                                           Sept 8

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.

Page 14: Lecture 4                                                                           Sept 8

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.

Page 15: Lecture 4                                                                           Sept 8

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

Page 16: Lecture 4                                                                           Sept 8
Page 17: Lecture 4                                                                           Sept 8
Page 18: Lecture 4                                                                           Sept 8
Page 19: Lecture 4                                                                           Sept 8

Exercise: Write a one-line statement to compute the average of a set of numbers stored in vector x.

Page 20: Lecture 4                                                                           Sept 8

Exercise: Write a one-line statement to compute the average of a set of numbers stored in vector x.

Answer:

>> sum(x) / length(x)

Page 21: Lecture 4                                                                           Sept 8

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 <= j <= length (vector)

Page 22: Lecture 4                                                                           Sept 8

Replacement Rules

1. 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

2. 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.

Page 23: Lecture 4                                                                           Sept 8
Page 24: Lecture 4                                                                           Sept 8

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.

Page 25: Lecture 4                                                                           Sept 8
Page 26: Lecture 4                                                                           Sept 8

Exercise: Let u be a vector. Write a Matlab code segment that determines the number of occurrences of 7 in it.

Example:

>> u = [1 3 5 7 9 7 5 3 1];>> < your code here> >> ans = 2

Page 27: Lecture 4                                                                           Sept 8

Exercise: Let u be a vector. Write a Matlab code segment that determines the number of occurrences of 7 in it.

Example:

>> u = [1 3 5 7 9 7 5 3 1];>> < your code here> >> ans = 2

Answer: sum(u == 7)

Page 28: Lecture 4                                                                           Sept 8

Find function

Page 29: Lecture 4                                                                           Sept 8

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:

>> u = [1 8 3 12 6 9 11]>> < your code here>>> vans = 1 8 3 12>> wans = 6 9 11

Page 30: Lecture 4                                                                           Sept 8

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:

>> u = [1 8 3 12 6 9 11]>> v = u(1:find(u == max(u)))>> vans = 1 8 3 12

Page 31: Lecture 4                                                                           Sept 8

Exercise: Write a Matlab expression to determine if there are any negative numbers in a vector u.

Answer:

Page 32: Lecture 4                                                                           Sept 8

Exercise: Write a Matlab expression to determine if there are any negative numbers in a vector u.

Answer: sum(u < 0) > 0

Page 33: Lecture 4                                                                           Sept 8

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)

Page 34: Lecture 4                                                                           Sept 8

Arithmetic operations

Arithmetic operations:>> A = [2 5 7 1 3];

>> A + 5

ans =

7 10 12 6 8

>> A .* 2

ans =

4 10 14 2 6

>> B = -1:1:3

B =

-1 0 1 2 3

Page 35: Lecture 4                                                                           Sept 8

Arithmetic operations (continued)

>> A .* B % element-by-element multiplicationans =-2 0 7 2 9>> A * B % matrix multiplication!!??? Error using ==> mtimesInner matrix dimensions must agree.>> C = [1 2 3]C =1 2 3>> A .* C % A and C must have the same length??? Error using ==> timesMatrix dimensions must agree.

Page 36: Lecture 4                                                                           Sept 8

Logical operations>> A = [2 5 7 1 3];

>> B = [0 6 5 3 2];

>> A >= 5

ans =

0 1 1 0 0

>> A >= B

ans =

1 0 1 0 1

>> C = [1 2 3];

>> A > C

??? Error using ==> gt

Matrix dimensions must agree.

Page 37: Lecture 4                                                                           Sept 8

Logical operations (continued)

>> A = [true true false false];>> B = [true false true false];>> A & Bans =1 0 0 0>> A | Bans =1 1 1 0>> C = [1 0 0]; % NOT a logical vector>> A(C) % yes, you can index logical vectors,

but ...??? Subscript indices must either be real positive

integers or logical.

Page 38: Lecture 4                                                                           Sept 8

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.

Page 39: Lecture 4                                                                           Sept 8

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.