360 likes | 540 Views
Concatenation. MATLAB lets you construct a new vector by concatenating other vectors: A = [B C D ... X Y Z] where the individual items in the brackets may be any vector defined as a constant or variable, and the length of A will be the sum of the lengths of the individual vectors.
E N D
Concatenation • MATLAB lets you construct a new vector by concatenating other vectors: • A = [B C D ... X Y Z] where the individual items in the brackets may be any vector defined as a constant or variable, and the length of A will be the sum of the lengths of the individual vectors. • A = [1 2 3 42] is a special case where all the component elements are scalar quantities.
Slicing (generalized indexing) • A(4) actually creates an anonymous 1 × 1 index vector, 4, and then using it to extract the specified element from the array A. • In general, B(<rangeB>) = A(<rangeA>) where <rangeA> and <rangeB> are both index vectors, A is an existing array, and B can be an existing array, a new array, or absent altogether (giving B the name ans). The values in B at the indices in <rangeB> are assigned the values of A from <rangeA> .
Exercise: Write Matlab code to create an array oddv of odd indices from a vector v. For example: >> v = [1 4 9 16 25] >> oddv = < your code here> >> oddv ans = [1 9 25] etc.
4.2. Matrices Example: The following 2 x 3 matrix (matA) can be created in Matlab as follows: Dimension of a matrix can be accessed by function called size.
Matrix operations Matrix addition, multiplication, inverse, determinant etc.
Matrix operations Matrix addition, multiplication, inverse, determinant, transpose etc.
Exercise: Solve a linear system of equations: 3x + 5y – 6z= 11 4x – 6y + z = 9 -2x + 3y + 5z = –13
Discussions and exercises, Chapter 4 Exercise 4.1
Exercise 4.2 • Write statements to do the following operations on a vector x: • Return the odd indexed elements.
Exercise 4.2 Write statements to do the following operations on a vector x: 2) Return the first half of x.
Exercise 4.2 Write statements to do the following operations on a vector x: 3) Return the vector in the reverse order.
Exercise 4.3 Given a vector v, and a vector k of indices, write a one or two statement code in Matlab that removes the elements of v in positions specified by k. Example: >> v = [1, 3, 5, 7, 11, 9, 19] >> k = [2, 4, 5] >> < your code here> >> v ans = 1, 5, 9, 19
Exercise 4.3 Given a vector v, and a vector k of indices, write a one or two statement code in Matlab that removes the elements of v in positions specified by k.
Exercise 4.4 what does Matlab output for the following commands? 1) 6 ~= 1 : 10 2) (6 ~= 1) : 10
Exercise 4.4 what does Matlab output for the following commands? 1) 6 ~= 1 : 10 2) (6 ~= 1) : 10
Exercise 4.5. (This is quite tricky, especially without using a loop construct like while or for.) Write a statement to return the elements of a vector randomly shuffled. Hint provided is a useful one. First understand how sort function works.
Array Manipulation We consider the following basic operations on vectors: • Creating an array • Extracting data from an array by indexing • Shortening an array • Mathematical and logical operations on arrays
Creating an Array – Constant Values • Entering the values directly, e.g. A = [2, 5, 7; 1, 3, 42] the semicolon identifies the next row, as would a new line in the command • Using the functions zeros( rows, cols),ones(rows, cols), rand(rows, cols) and randn(rows, cols) to create vectors filled with 0, 1, or random values between 0 and 1
Indexing an Array • The process of extracting values from an array, or inserting values into an array • Syntax: • A(row, col) returns the element(s) at the location(s) specified by the array row and column indices. • A(row, col) = value replaces the elements at the location(s) specified by the array row and column indices. • The indexing row and column vectors may contain either numerical or logical values
Operating on Arrays Four techniques extend directly from operations on vectors: ■ Arithmetic operations ■ Logical operations ■ Applying library functions ■ Slicing (generalized indexing) The following deserves an additional word because of the nature of arrays: ■ Concatenation
Array Concatenation • Array concatenation can be accomplished horizontally or vertically: • R = [A B C] succeeds as long as A, B and C have the same number of rows; the columns in R will be the sum of the columns in A, B and C. • R = [A; B; C] succeeds as long as A, B and C have the same number of columns; the rows in R will be the sum of the rows in A, B and C.
Reshaping Arrays • Arrays are actually stored in column order in Matlab. So internally, a 2 × 3 array is stored as a column vector: A(1,1) A(2,1) A(1,2) A(2,2) A(1,3) A(2,3) • Any n × m array can be reshaped into any p × q array as long as n*m = p*q using the reshape function.
Engineering Example—Computing Soil Volume • Consider the example where you are given the depth of soil from a survey in the form of a rectangular array of soil depth. • You are also given the footprint of the foundations of a building to be built on that site and the depth of the foundation. • Compute the volume of soil to be removed.
Solution clear clc % soil depth data for each square produced % by the survey dpth = [8 8 9 8 8 8 8 8 7 8 7 7 7 7 8 8 8 7 8 8 8 8 8 8 8 7 7 7 7 7 8 7 8 8 8 7 . . . 9 8 8 7 7 8 7 7 7 7 8 8 9 9 9 8 7 8]; % estimated proportion of each square that should % be excavated area = [1 1 1 1 1 1 1 1 1 1 .3 0 0 0 0 0 0 0 . . . 0 0 0 0 0 0 .4 .8 .9 1 1 1 1 1 1 1 1 .6]; square_volume = dpth .* area; total_soil = sum(sum(square_volume))
Summary This chapter introduced you to vectors and arrays. For each collection, you saw how to: ■ Create them by concatenation and a variety of special-purpose functions ■ Access and remove elements, rows, or columns ■ Perform mathematical and logical operations on them ■ Apply library functions, including those that summarize whole columns or rows ■ Move arbitrary selected rows and columns from one array to another ■ Reshape and linearize arrays