lecture 4 sept 7 chapter 4. chapter 4 – arrays, collections and indexing this chapter discusses...

33
Lecture 4 Sept 7 • Chapter 4

Post on 22-Dec-2015

227 views

Category:

Documents


5 download

TRANSCRIPT

Page 1: Lecture 4 Sept 7 Chapter 4. Chapter 4 – arrays, collections and indexing This chapter discusses the basic calculations involving rectangular collections

Lecture 4 Sept 7

• Chapter 4

Page 2: Lecture 4 Sept 7 Chapter 4. Chapter 4 – arrays, collections and indexing This chapter discusses the basic calculations involving rectangular collections

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 3: Lecture 4 Sept 7 Chapter 4. Chapter 4 – arrays, collections and indexing This chapter discusses the basic calculations involving rectangular collections

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 4: Lecture 4 Sept 7 Chapter 4. Chapter 4 – arrays, collections and indexing This chapter discusses the basic calculations involving rectangular collections

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 5: Lecture 4 Sept 7 Chapter 4. Chapter 4 – arrays, collections and indexing This chapter discusses the basic calculations involving rectangular collections

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

Support for vector processing is a distinct feature of Matlab and it is where it differs significantly from languages like c++ and Java.

Page 6: Lecture 4 Sept 7 Chapter 4. Chapter 4 – arrays, collections and indexing This chapter discusses the basic calculations involving rectangular collections

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 7: Lecture 4 Sept 7 Chapter 4. Chapter 4 – arrays, collections and indexing This chapter discusses the basic calculations involving rectangular collections

Problem: Write a code segment in Matlab to generate a sequence containing the outcomes of 50 independent rolls of a fair die.

We need 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}.

Need a Matlab function to generate random numbers.

rand() gives a random real number between 0 and 1.

Page 8: Lecture 4 Sept 7 Chapter 4. Chapter 4 – arrays, collections and indexing This chapter discusses the basic calculations involving rectangular collections

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 (when the number N of rolls is large.)

Page 9: Lecture 4 Sept 7 Chapter 4. Chapter 4 – arrays, collections and indexing This chapter discusses the basic calculations involving rectangular collections

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 10: Lecture 4 Sept 7 Chapter 4. Chapter 4 – arrays, collections and indexing This chapter discusses the basic calculations involving rectangular collections

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 11: Lecture 4 Sept 7 Chapter 4. Chapter 4 – arrays, collections and indexing This chapter discusses the basic calculations involving rectangular collections
Page 12: Lecture 4 Sept 7 Chapter 4. Chapter 4 – arrays, collections and indexing This chapter discusses the basic calculations involving rectangular collections
Page 13: Lecture 4 Sept 7 Chapter 4. Chapter 4 – arrays, collections and indexing This chapter discusses the basic calculations involving rectangular collections
Page 14: Lecture 4 Sept 7 Chapter 4. Chapter 4 – arrays, collections and indexing This chapter discusses the basic calculations involving rectangular collections

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

Page 15: Lecture 4 Sept 7 Chapter 4. Chapter 4 – arrays, collections and indexing This chapter discusses the basic calculations involving rectangular collections

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 16: Lecture 4 Sept 7 Chapter 4. Chapter 4 – arrays, collections and indexing This chapter discusses the basic calculations involving rectangular collections

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)

–For replacing elements, all index values j must be

j <= element

Page 17: Lecture 4 Sept 7 Chapter 4. Chapter 4 – arrays, collections and indexing This chapter discusses the basic calculations involving rectangular collections

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 18: Lecture 4 Sept 7 Chapter 4. Chapter 4 – arrays, collections and indexing This chapter discusses the basic calculations involving rectangular collections
Page 19: Lecture 4 Sept 7 Chapter 4. Chapter 4 – arrays, collections and indexing This chapter discusses the basic calculations involving rectangular collections

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

• Logical vectors in Matlab echo in the Command window as 1 or 0, they represent T and F respectively.

Page 20: Lecture 4 Sept 7 Chapter 4. Chapter 4 – arrays, collections and indexing This chapter discusses the basic calculations involving rectangular collections
Page 21: Lecture 4 Sept 7 Chapter 4. Chapter 4 – arrays, collections and indexing This chapter discusses the basic calculations involving rectangular collections

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 22: Lecture 4 Sept 7 Chapter 4. Chapter 4 – arrays, collections and indexing This chapter discusses the basic calculations involving rectangular collections

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: u(u == 7)/7

Page 23: Lecture 4 Sept 7 Chapter 4. Chapter 4 – arrays, collections and indexing This chapter discusses the basic calculations involving rectangular collections

Find function

Page 24: Lecture 4 Sept 7 Chapter 4. Chapter 4 – arrays, collections and indexing This chapter discusses the basic calculations involving rectangular collections

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 25: Lecture 4 Sept 7 Chapter 4. Chapter 4 – arrays, collections and indexing This chapter discusses the basic calculations involving rectangular collections

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 26: Lecture 4 Sept 7 Chapter 4. Chapter 4 – arrays, collections and indexing This chapter discusses the basic calculations involving rectangular collections

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

Answer:

Page 27: Lecture 4 Sept 7 Chapter 4. Chapter 4 – arrays, collections and indexing This chapter discusses the basic calculations involving rectangular collections

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

Answer:

>> u = [2, -4, 5, -3, 8, 11];

>> any(u < 0)

ans =

1

You can count the number of negative numbers in a vector using sum(u < 0).

Page 28: Lecture 4 Sept 7 Chapter 4. Chapter 4 – arrays, collections and indexing This chapter discusses the basic calculations involving rectangular collections

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 29: Lecture 4 Sept 7 Chapter 4. Chapter 4 – arrays, collections and indexing This chapter discusses the basic calculations involving rectangular collections

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 30: Lecture 4 Sept 7 Chapter 4. Chapter 4 – arrays, collections and indexing This chapter discusses the basic calculations involving rectangular collections

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 31: Lecture 4 Sept 7 Chapter 4. Chapter 4 – arrays, collections and indexing This chapter discusses the basic calculations involving rectangular collections

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 32: Lecture 4 Sept 7 Chapter 4. Chapter 4 – arrays, collections and indexing This chapter discusses the basic calculations involving rectangular collections

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 33: Lecture 4 Sept 7 Chapter 4. Chapter 4 – arrays, collections and indexing This chapter discusses the basic calculations involving rectangular collections

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 ormaximum value in a vector, plus the position in that vectorwhere that value occurred.

■ round(v), ceil(v), floor(v) remove the fractional part of the numbers in a vector by conventional rounding, rounding up, rounding down, respectively.

We can also apply functions like sqrt or sine or cosine to each element of a vector.