ADVANCED MATRIX OPERATIONS PART II - PowerPoint PPT Presentation

1 / 18
About This Presentation
Title:

ADVANCED MATRIX OPERATIONS PART II

Description:

ADVANCED MATRIX OPERATIONS - PART II. June 1, 99. ADVANCED SUBSCRIPTING ... all the rows of a but columns defined by v, i.e. columns 1 and 3. Try it! ... – PowerPoint PPT presentation

Number of Views:52
Avg rating:3.0/5.0
Slides: 19
Provided by: BijanMo3
Category:

less

Transcript and Presenter's Notes

Title: ADVANCED MATRIX OPERATIONS PART II


1
ADVANCED MATRIX OPERATIONS - PART II
  • June 1, 99

2
ADVANCED SUBSCRIPTING
  • You can pick out the elements of an array A using
    another array
  • Define
  • a1 2 34 5 67 8 9,
  • v1 3
  • When you see a(,v) read it like this
  • all the rows of a but columns defined by v, i.e.
    columns 1 and 3

3
Try it!
  • Using a and v just defined, try the following
    command
  • a(v,) and a(,v)
  • Look at what you get. Does it make sense based on
    the definition?

4
MAKING AN ARRAY OUT OF A MATRIX
  • Using colon by itself strings out all the
    elements of A in a long column vector
  • In other words, if you have a 5x5 matrix a, a()
    gives you a 25x1 column vector
  • Try a()
  • A more interesting effect is observed by the
    following
  • A()1120

5
Try it!
  • Define a 5X5 matrix using rand. Then
  • Use a() and see what you get
  • More interestingly try this a()125
  • The second step above is particularly
    significant. Look at it and see if you see a
    pattern

6
The reshape command
  • Colon operator took a, say 6x6, matrix and turned
    it into 36x1 array using a().
  • reshape can take a 12x1 array and rearrange it as
    2x6 or 3x4, 6x2. The catch is that the rearranged
    matrix and the original must have, naturally, the
    same number of elements
  • The question is how does reshape work? See next
    slide

7
REARRANGING MATRICES
  • It is possible to rearrange a, say, 3x4 matrix
    into a 2x6, 1x12, 4x3 etc. as long as number of
    elements do not change
  • Breshape(A,m,n)
  • maps A into an m rows, n columns matrix
  • 1 2 3 4
  • 5 6 7 8
  • 9 10 11 12
  • 13 14 15 16

1 9 2 10 3 11 4 12 5 13 6 14 7 15 8 16
reshape(A,2,8)
8
Working of reshape
9
Practical use of reshape
  • A popular signal is alternating 1s and -1s
  • 1 -1 1 -1 1 -1...1 -1
  • How can you create an array like this of length
    1000?
  • Hint first create two separate arrays, 1s and
    -1s, then mix them and use reshape

10
sum command
  • A very powerful command is sum.
  • What sum does is to add all the elements of an
    array. For example if a1 2 3 4 5, then
    sum(a)15.
  • Now, if a is
  • sum(a) sums each column giving a 1x8 array
  • sum(a)6 22 8 24 10 26 12 28

1 9 2 10 3 11 4 12 5 13 6 14 7 15 8 16
11
EQUATING MATRICES
  • It is important that when matrices or arrays are
    equated, the number of rows and columns match on
    both sides
  • For example, if a is 3x3
  • a()ones(1,10) is invalid because the left hand
    side is 9x1 but the right hand side is 1x10.
  • The correct assignment is
  • a()ones(1,9)
  • Note the use of apostrophe for transposition

12
Try it!
  • Define a 5x5 matrix using rand. Then set all 25
    elements equal to 7 using an assignment statement

13
EMPTY MATRICES
  • Statement x defines a zero x zero matrix, an
    empty matrix. This is different from clear x.
  • We can use an empty matrix to efficiently remove
    rows and columns from a matrix.
  • Do next slide to see how this works
  • In a 5x5 matrix, what does this statement do?
  • a(, 2 3)

14
Removing rows or columns of matrices
  • In a 5x5 matrix, what does this statement do?
  • a (,2 3)
  • How about a ( 2 3,)

15
MATRIX MANIPULATION
  • Here is a list of interesting operators. Try a
    few
  • rot90...............rotates a matrix by 90 dgrees
  • fliplr................flip matrix/array left to
    right
  • flipud..............flip matrix/array up and
    down
  • diag.................extract diagonal

16
USEFUL MATLAB FUNCTIONS
  • The following functions are quite handy. Try them
    on a random vector of numbers of length 100.
  • max(v)............find the maximum of v
  • min(v)............find the minimum of v
  • mean(v).........find the mean(average) of v
  • std(v).............find the standard deviation
  • sort(v)............sort v
  • sum(v)...........sum of all the elements in v
  • prod(v)..........product of elements in v
  • hist(v)............histogram of valus in v

17
WORKING WITH SOUND
  • Lets try out the previous commands on an actual
    sound file.
  • From the web page download and save an audio file
    called bond.mat. Place it in a directory MATLAB
    can see and type load bond
  • Type sound(m,8000) and test if you hear anything
  • Also, plot it using plot(m)

18
Homework
  • After loading bond, write a code to do the
    following tasks (one line per question! )
  • Play bond backwards
  • How many samples are in bond?
  • Find the peak amplitude and its location
  • How many times does the signal amplitude exceed
    0.6 volts
  • Set all those amplitudes to 0.6
  • Plot your result to verify these changes
Write a Comment
User Comments (0)
About PowerShow.com