Concatenation - PowerPoint PPT Presentation

About This Presentation
Title:

Concatenation

Description:

... lets you construct a new vector by concatenating other ... Move arbitrary selected rows and columns from one array to another. Reshape and linearize arrays ... – PowerPoint PPT presentation

Number of Views:51
Avg rating:3.0/5.0
Slides: 37
Provided by: engineeri9
Category:

less

Transcript and Presenter's Notes

Title: Concatenation


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(ltrangeBgt) A(ltrangeAgt)
  • where ltrangeAgt and ltrangeBgt 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 ltrangeBgt are assigned the values of A
    from ltrangeAgt .

3
Example
4
Exercise Write Matlab code to create an array
oddv of odd indices from a vector v. For
example gtgt v 1 4 9 16 25 gtgt oddv lt your
code heregt gtgt 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
(No Transcript)
13
Exercise Solve a linear system of equations
3x 5y 6z 11 4x 6y z
9 -2x 3y 5z 13
14
Sum, max, min, size etc.
15
4.3. Mixed Data Types
16
Discussions and exercises, Chapter 4
Exercise 4.1
17
(No Transcript)
18
  • Exercise 4.2
  • Write statements to do the following operations
    on a vector x
  • Return the odd indexed elements.

19
Exercise 4.2 Write statements to do the
following operations on a vector x 2) Return
the first half of x.
20
Exercise 4.2 Write statements to do the
following operations on a vector x 3) Return
the vector in the reverse order.
21
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 gtgt v 1, 3,
5, 7, 11, 9, 19 gtgt k 2, 4, 5 gtgt lt your code
heregt gtgt v ans 1, 5, 9, 19
22
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.
23
Exercise 4.4 what does Matlab output for the
following commands? 1) 6 1 10 2) (6
1) 10
24
Exercise 4.4 what does Matlab output for the
following commands? 1) 6 1 10 2) (6
1) 10
25
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.
26
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

27
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

28
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

29
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

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

31
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 nm pq using the reshape
    function.

32
Engineering ExampleComputing 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.

33
Survey Data
34
Building Footprint
35
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))

36
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
Write a Comment
User Comments (0)
About PowerShow.com