Concatenation

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

description

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. - PowerPoint PPT Presentation

Transcript of Concatenation

Page 1: 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.– A = [1 2 3 42]is a special case where all the component elements are scalar quantities.

Page 2: Concatenation

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

Example:

Page 4: Concatenation

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

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

Accessing and modifying array elements

Page 7: Concatenation

Accessing and modifying array elements

Page 8: Concatenation

Accessing and modifying array elements

Page 9: Concatenation

Matrix operationsMatrix addition, multiplication, inverse, determinant etc.

Page 10: Concatenation

Matrix operationsMatrix addition, multiplication, inverse, determinant, transpose etc.

Page 11: Concatenation

Logical indexing in 2-dim matrices

Page 12: Concatenation
Page 13: Concatenation

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

Page 14: Concatenation

Sum, max, min, size etc.

Page 15: Concatenation

4.3. Mixed Data Types

Page 16: Concatenation

Discussions and exercises, Chapter 4

Exercise 4.1

Page 17: Concatenation
Page 18: Concatenation

Exercise 4.2

Write statements to do the following operations on a vector x:

1)Return the odd indexed elements.

Page 19: Concatenation

Exercise 4.2

Write statements to do the following operations on a vector x:

2) Return the first half of x.

Page 20: Concatenation

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

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

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

Exercise 4.4 what does Matlab output for the following commands?

1) 6 ~= 1 : 10

2) (6 ~= 1) : 10

Page 24: Concatenation

Exercise 4.4 what does Matlab output for the following commands?

1) 6 ~= 1 : 10

2) (6 ~= 1) : 10

Page 25: Concatenation

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

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

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

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

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

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

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

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

Survey Data

Page 34: Concatenation

Building Footprint

Page 35: Concatenation

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

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