390 likes | 401 Views
Learn how to create, manipulate, access, and perform mathematical and logical operations on MATLAB vectors and arrays. Understand the concepts of homogeneous collections, vector manipulation, and indexing.
E N D
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 <= b <= c. Answer: Exercise 3.12: How many leap years are between 1780 and 2832, inclusive? Recall the definition of a leap year.
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
Concept: Data Collections This section considers two very common ways to group data: in arrays and in vectors. Data Abstractionallows usto 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 Collectionis constrained to accept only items of the same data type – in this case, they will all be numbers
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.
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
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
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.
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.
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.
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
Exercise: Write a one-line statement to compute the average of a set of numbers stored in vector x.
Exercise: Write a one-line statement to compute the average of a set of numbers stored in vector x. Answer: >> sum(x) / length(x)
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)
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.
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.
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
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)
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> >> v ans = 1 8 3 12 >> w ans = 6 9 11
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
Exercise: Write a Matlab expression to determine if there are any negative numbers in a vector u. Answer:
Exercise: Write a Matlab expression to determine if there are any negative numbers in a vector u. Answer: sum(u < 0) > 0
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)
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
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.
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.
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.
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.
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.