Concatenation
-
Upload
dale-parsons -
Category
Documents
-
view
13 -
download
0
description
Transcript of Concatenation
![Page 1: Concatenation](https://reader035.fdocuments.in/reader035/viewer/2022062719/56813061550346895d962d8a/html5/thumbnails/1.jpg)
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.
![Page 2: Concatenation](https://reader035.fdocuments.in/reader035/viewer/2022062719/56813061550346895d962d8a/html5/thumbnails/2.jpg)
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> .
![Page 3: Concatenation](https://reader035.fdocuments.in/reader035/viewer/2022062719/56813061550346895d962d8a/html5/thumbnails/3.jpg)
Example:
![Page 4: Concatenation](https://reader035.fdocuments.in/reader035/viewer/2022062719/56813061550346895d962d8a/html5/thumbnails/4.jpg)
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>>> oddvans = [1 9 25]
etc.
![Page 5: Concatenation](https://reader035.fdocuments.in/reader035/viewer/2022062719/56813061550346895d962d8a/html5/thumbnails/5.jpg)
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.
![Page 6: Concatenation](https://reader035.fdocuments.in/reader035/viewer/2022062719/56813061550346895d962d8a/html5/thumbnails/6.jpg)
Accessing and modifying array elements
![Page 7: Concatenation](https://reader035.fdocuments.in/reader035/viewer/2022062719/56813061550346895d962d8a/html5/thumbnails/7.jpg)
Accessing and modifying array elements
![Page 8: Concatenation](https://reader035.fdocuments.in/reader035/viewer/2022062719/56813061550346895d962d8a/html5/thumbnails/8.jpg)
Accessing and modifying array elements
![Page 9: Concatenation](https://reader035.fdocuments.in/reader035/viewer/2022062719/56813061550346895d962d8a/html5/thumbnails/9.jpg)
Matrix operationsMatrix addition, multiplication, inverse, determinant etc.
![Page 10: Concatenation](https://reader035.fdocuments.in/reader035/viewer/2022062719/56813061550346895d962d8a/html5/thumbnails/10.jpg)
Matrix operationsMatrix addition, multiplication, inverse, determinant, transpose etc.
![Page 11: Concatenation](https://reader035.fdocuments.in/reader035/viewer/2022062719/56813061550346895d962d8a/html5/thumbnails/11.jpg)
Logical indexing in 2-dim matrices
![Page 12: Concatenation](https://reader035.fdocuments.in/reader035/viewer/2022062719/56813061550346895d962d8a/html5/thumbnails/12.jpg)
![Page 13: Concatenation](https://reader035.fdocuments.in/reader035/viewer/2022062719/56813061550346895d962d8a/html5/thumbnails/13.jpg)
Exercise: Solve a linear system of equations: 3x + 5y – 6z= 11 4x – 6y + z = 9 -2x + 3y + 5z = –13
![Page 14: Concatenation](https://reader035.fdocuments.in/reader035/viewer/2022062719/56813061550346895d962d8a/html5/thumbnails/14.jpg)
Sum, max, min, size etc.
![Page 15: Concatenation](https://reader035.fdocuments.in/reader035/viewer/2022062719/56813061550346895d962d8a/html5/thumbnails/15.jpg)
4.3. Mixed Data Types
![Page 16: Concatenation](https://reader035.fdocuments.in/reader035/viewer/2022062719/56813061550346895d962d8a/html5/thumbnails/16.jpg)
Discussions and exercises, Chapter 4
Exercise 4.1
![Page 17: Concatenation](https://reader035.fdocuments.in/reader035/viewer/2022062719/56813061550346895d962d8a/html5/thumbnails/17.jpg)
![Page 18: Concatenation](https://reader035.fdocuments.in/reader035/viewer/2022062719/56813061550346895d962d8a/html5/thumbnails/18.jpg)
Exercise 4.2
Write statements to do the following operations on a vector x:
1)Return the odd indexed elements.
![Page 19: Concatenation](https://reader035.fdocuments.in/reader035/viewer/2022062719/56813061550346895d962d8a/html5/thumbnails/19.jpg)
Exercise 4.2
Write statements to do the following operations on a vector x:
2) Return the first half of x.
![Page 20: Concatenation](https://reader035.fdocuments.in/reader035/viewer/2022062719/56813061550346895d962d8a/html5/thumbnails/20.jpg)
Exercise 4.2
Write statements to do the following operations on a vector x:
3) Return the vector in the reverse order.
![Page 21: Concatenation](https://reader035.fdocuments.in/reader035/viewer/2022062719/56813061550346895d962d8a/html5/thumbnails/21.jpg)
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>>> vans = 1, 5, 9, 19
![Page 22: Concatenation](https://reader035.fdocuments.in/reader035/viewer/2022062719/56813061550346895d962d8a/html5/thumbnails/22.jpg)
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.
![Page 23: Concatenation](https://reader035.fdocuments.in/reader035/viewer/2022062719/56813061550346895d962d8a/html5/thumbnails/23.jpg)
Exercise 4.4 what does Matlab output for the following commands?
1) 6 ~= 1 : 10
2) (6 ~= 1) : 10
![Page 24: Concatenation](https://reader035.fdocuments.in/reader035/viewer/2022062719/56813061550346895d962d8a/html5/thumbnails/24.jpg)
Exercise 4.4 what does Matlab output for the following commands?
1) 6 ~= 1 : 10
2) (6 ~= 1) : 10
![Page 25: Concatenation](https://reader035.fdocuments.in/reader035/viewer/2022062719/56813061550346895d962d8a/html5/thumbnails/25.jpg)
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.
![Page 26: Concatenation](https://reader035.fdocuments.in/reader035/viewer/2022062719/56813061550346895d962d8a/html5/thumbnails/26.jpg)
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
![Page 27: Concatenation](https://reader035.fdocuments.in/reader035/viewer/2022062719/56813061550346895d962d8a/html5/thumbnails/27.jpg)
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
![Page 28: Concatenation](https://reader035.fdocuments.in/reader035/viewer/2022062719/56813061550346895d962d8a/html5/thumbnails/28.jpg)
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
![Page 29: Concatenation](https://reader035.fdocuments.in/reader035/viewer/2022062719/56813061550346895d962d8a/html5/thumbnails/29.jpg)
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
![Page 30: Concatenation](https://reader035.fdocuments.in/reader035/viewer/2022062719/56813061550346895d962d8a/html5/thumbnails/30.jpg)
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.
![Page 31: Concatenation](https://reader035.fdocuments.in/reader035/viewer/2022062719/56813061550346895d962d8a/html5/thumbnails/31.jpg)
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.
![Page 32: Concatenation](https://reader035.fdocuments.in/reader035/viewer/2022062719/56813061550346895d962d8a/html5/thumbnails/32.jpg)
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.
![Page 33: Concatenation](https://reader035.fdocuments.in/reader035/viewer/2022062719/56813061550346895d962d8a/html5/thumbnails/33.jpg)
Survey Data
![Page 34: Concatenation](https://reader035.fdocuments.in/reader035/viewer/2022062719/56813061550346895d962d8a/html5/thumbnails/34.jpg)
Building Footprint
![Page 35: Concatenation](https://reader035.fdocuments.in/reader035/viewer/2022062719/56813061550346895d962d8a/html5/thumbnails/35.jpg)
Solutionclearclc% soil depth data for each square produced % by the surveydpth = [8 8 9 8 8 8 8 8 7 8 7 7 7 7 8 8 8 78 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 excavatedarea = [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))
![Page 36: Concatenation](https://reader035.fdocuments.in/reader035/viewer/2022062719/56813061550346895d962d8a/html5/thumbnails/36.jpg)
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