Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Lecture 4 Sept 7 Chapter 4. Chapter 4 – arrays, collections and indexing This chapter discusses the basic calculations involving rectangular collections."— Presentation transcript:

1 Lecture 4 Sept 7 Chapter 4

2 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

3 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

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

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

6 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

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

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

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

10 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

11

12

13

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

15 Answer: >> sum(x) / length(x)

16 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

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

18

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

20

21 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]; >> >> ans = 2

22 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]; >> >> ans = 2 Answer: u(u == 7)/7

23 Find function

24 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 ans = 1 8 3 12 >> w ans = 6 9 11

25 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))) >> v ans = 1 8 3 12

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

27 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).

28 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)

29 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

30 Arithmetic operations (continued) >> A.* B % element-by-element multiplication ans = -2 0 7 2 9 >> A * B % matrix multiplication!! ??? Error using ==> mtimes Inner matrix dimensions must agree. >> C = [1 2 3] C = 1 2 3 >> A.* C % A and C must have the same length ??? Error using ==> times Matrix dimensions must agree.

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

32 Logical operations (continued) >> A = [true true false false]; >> B = [true false true false]; >> A & B ans = 1 0 0 0 >> A | B ans = 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.

33 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. ■ 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.


Download ppt "Lecture 4 Sept 7 Chapter 4. Chapter 4 – arrays, collections and indexing This chapter discusses the basic calculations involving rectangular collections."

Similar presentations


Ads by Google