1 / 36

Concatenation

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.

ariane
Download Presentation

Concatenation

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

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

  3. Example:

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

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

  6. Accessing and modifying array elements

  7. Accessing and modifying array elements

  8. Accessing and modifying array elements

  9. Matrix operations Matrix addition, multiplication, inverse, determinant etc.

  10. Matrix operations Matrix addition, multiplication, inverse, determinant, transpose etc.

  11. Logical indexing in 2-dim matrices

  12. Exercise: Solve a linear system of equations: 3x + 5y – 6z= 11 4x – 6y + z = 9 -2x + 3y + 5z = –13

  13. Sum, max, min, size etc.

  14. 4.3. Mixed Data Types

  15. Discussions and exercises, Chapter 4 Exercise 4.1

  16. Exercise 4.2 • Write statements to do the following operations on a vector x: • Return the odd indexed elements.

  17. Exercise 4.2 Write statements to do the following operations on a vector x: 2) Return the first half of x.

  18. Exercise 4.2 Write statements to do the following operations on a vector x: 3) Return the vector in the reverse order.

  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. Example: >> v = [1, 3, 5, 7, 11, 9, 19] >> k = [2, 4, 5] >> < your code here> >> v ans = 1, 5, 9, 19

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

  21. Exercise 4.4 what does Matlab output for the following commands? 1) 6 ~= 1 : 10 2) (6 ~= 1) : 10

  22. Exercise 4.4 what does Matlab output for the following commands? 1) 6 ~= 1 : 10 2) (6 ~= 1) : 10

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

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

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

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

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

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

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

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

  31. Survey Data

  32. Building Footprint

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

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

More Related