Call Now: (800) 537-1660  
The Algebra Buster
The Algebra Buster


May 24th









May 24th

ADVANCED MATRIX OPERATIONS

ADVANCED MATRIX OPE RATIONS

• Advanced matrix operations fall under the
fol lowing categories .

• Building larger matrices
• Relational operations (larger/smaller)
Logical operators (and ,or)
• Subscripting
• Manipulating matrices
• Reshaping

BUILDING LARGER MATRICES

• We can form larger matrices from smaller
matrices.

• Let a=[1 2 3;4 5 6;7 8 9] and b=[4 5 6] then
c=[a,b’]

adds a 4th column to a. c looks like this . Try
this and see for yourself
1 2 3 4
4 5 6 5
7 8 9 6

MATRIX MULTIPLICATION

• MATLAB multiplies two matrices provided
rows and column numbers agree .
• Let

A=[1 2 3;4 5 6;7 8 9]
B=[7 8 9;4 5 6;1 2 3]

• Multiplication of A by B is simply A *B. The
answer is

Try it!

• To ap preciate size restrictions, try to
multiply a=[1 2 3] by b=[4 5 6] using a*b.
What do you get and why?.
• Try the multiplication given in the previous
slide and check your answer.
• Now do a term-by- term multiplication like
a.*b. Explain why this is different from
a*b?

RELATIONAL OPERATIONS

• Relational operators are used for
comparison of two arrays . They are defined
as follows

< less than <= less than or equal
> greater than >= greater than or equal
= = equal ~= not equal

WHAT DO THEY DO?

• The response of a relational operator is
another vector
• This vector is binary (0,1) and of the same
length as the data vector
• The 1 locations indicate places where the
relational operator is TRUE

Example

• We have two arrays

• If you say c=a>b, then

Try it!

• Define the following row vector
v=[1 3 4 9 8 6 7 0 2 6]

• Do the following and look at the result.
Explain what is happening.

x=v<5, x=v<=4, x=v>3, x=v>=0, x=v==6
x=v~=6

How many times a was larger
than b? introducing sum

• Application: compare the closing prices of
two stocks over a one year period. How
many times one closed higher than the
other?

sum(x) sums of all elements of x and
produces one number
• Let x=[1 2 3 4 5], then sum(x)=15

sum over a matrix

• If x is an NxN matrix, then sum(x) is a 1xN
array. Each term is the sum of the
corresponding column

Stock comparison question

• Let stockA and stockB be 1x365 arrays.
Define
comp=[stockA>stockB]

• Comp is now a 0/1 array with 1 anytime A
exceeded B. Connect the dots. How many
days did A closed higher than B?

find command

• There are many instances where we want
to know the locations of non-zero entries
• For example, v > 5 is a logical array where
all non-zero entries correspond to positions
where v exceeds 5
find(v>5) gives the exact locations where
the elements of v exceed 5

Try it!

• Take the v vector defined in slide 9. Find
all locations where v is
• Less than 5
• Larger than 3
• Less than or equal 9

LOGICAL OPERATORS

• Logical expressions can be compared
using logical operators. There are 3 logical
operators:
not...~
and...&
or......|

Using logical AND (&)

• Let’s try the & operator. Define
a=[1 9 8], b=[ 2 9 7] and c=[ 2 5 4 ].
• If we say a>b we get [0 0 1] which says
that in the third position, a>b is true. a>c
gets us [ 0 1 1].
• What we want is this: where both of these
expressions are true at the same time
[a>b & a>c]

The answer is...

• Here is the code
a=[1 9 8];
b=[ 2 9 7];
c=[ 2 5 4 ];
p=[a>b&a>c]

• p is then equal to [0 0 1]. Notice that in
the third position the joint condition was
true

Using logical OR ( | )

• The vertical bar is logical OR. a>b | a>c
looks for locations where a>b OR a>c is
true
• The answer is the 2nd and 3rd locations
where meets the condition

How many times a signal exceeds
a fixed threshold?

Random number generator

• We frequently need numbers to test things.
• One easy way of getting them is through
random number generators
• rand (1,N) generates N random numbers
between 0 and 1
• rand (N) generates an NXN matrix of
random numbers between 0 and 1

Try it!

• Give these commands a try in MATLAB and
see what you get
• rand(1,10)
• rand(10,1)
• rand(5)
• rand(3,4)

Using find: removing noise spike
from data

• First we generate a 1000 point signal then
plant a spike right in the middle. The code
to do this:
• x=rand(1,1000);
• x(500)=5;
• plot(x)

Graph of spike

Sample code

In-class practice

• Generate a 1x1000 array of normal random
numbers using randn
• Find all locations (indices) where x>.6
• How many times does x exceed .3?
• How many times does does x lie
between 0.2 and 0.4?
• How many times does x exceed .8 or
drop below .2?

Importing data

• Data that is saved as .mat file can be read
by Matlab using load command
• For example we have bond.mat on the
server. When you do load bond.mat, your
workspace will show an array m which
contains the imported data

Homework

• Load bond.mat. This is an audio clip
sampled at 8000 samples/sec.
Plot its amplitude vs. time( not number of samples)
• Find how many times amplitude exceeds 0.2 volts
• Find how many times amplitude lies between -0.1 and
+0.2 volts
• At what times does the amplitude goes above 0.2
volts?

Prev Next
 
Home    Why Algebra Buster?    Guarantee    Testimonials    Ordering    FAQ    About Us
What's new?    Resources    Animated demo    Algebra lessons    Bibliography of     textbooks
 

Copyright © 2009, algebra-online.com. All rights reserved.