CSE 1520 Computer Use: Fundamentals - PowerPoint PPT Presentation

About This Presentation
Title:

CSE 1520 Computer Use: Fundamentals

Description:

When the cumsum and cumprod functions are used ... v*3 ans = 3 9 15 21 Numerical or arithmetic operations can be performed on every element in the entire ... – PowerPoint PPT presentation

Number of Views:75
Avg rating:3.0/5.0
Slides: 29
Provided by: hof106
Category:

less

Transcript and Presenter's Notes

Title: CSE 1520 Computer Use: Fundamentals


1
EECS 1541 -- Introduction to Computing for the
Physical Sciences
  • Week 3 Vectors and Matrices (Part III)
  • READING 2.2 2.4

2
Vectors and Matrices as Function Arguments
EECS 1541 -- Introduction to Computing for the
Physical Sciences
  • In MATLAB, an entire vector or matrix can be
    passed as an argument to a function such as sum,
    prod
  • For a vector, the function will be evaluated on
    every element.

gtgt v 15 gtgt sum(v) ans 15
  • Example

Means 1 2 3 4 5
gtgt v 15 gtgt prod(v) ans 120
Means 1 2 3 4 5
3
Vectors and Matrices as Function Arguments
EECS 1541 -- Introduction to Computing for the
Physical Sciences
  • For matrices, the sum and prod functions operate
    on every individual column.
  • Hence, if a matrix has dimensions r x c, the
    result for the sum and prod functions will be a 1
    x c row vector
  • Example

gtgt A 13 24 57 gtgt sum(A) ans 8
11 14
from 3 4 7
from 1 2 5
from 2 3 6
4
Vectors and Matrices as Function Arguments
EECS 1541 -- Introduction to Computing for the
Physical Sciences
  • Example How would you compute the sum of all the
    entries in A?

gtgt A 13 24 57 gtgt
5
Vectors and Matrices as Function Arguments
EECS 1541 -- Introduction to Computing for the
Physical Sciences
  • Some other commonly used functions in array
    include

functions descriptions Examples in vectors
min Returns the smallest number gtgt v 1 2 3 4 gtgt min(v) ans 1
max Returns the largest number gtgt v 1 2 3 4 gtgt max(v) ans 4
cumsum Returns the cumulative sum gtgt v 1 2 3 4 gtgt cumsum(v) ans 1 3 6 10
cumprod Returns the cumulative product gtgt v 1 2 3 4 gtgt cumprod(v) ans 1 2 6 24
6
Vectors and Matrices as Function Arguments
EECS 1541 -- Introduction to Computing for the
Physical Sciences
  • Example How would you determine the largest
    number in the following matrix?

gtgt A 13 2-10 57
7
Vectors and Matrices as Function Arguments
EECS 1541 -- Introduction to Computing for the
Physical Sciences
  • When the cumsum and cumprod functions are used in
    matrices, they return the cumulative sum or
    product of every column
  • Hence, the resulting matrix will have the same
    dimension as the input matrix.
  • Example

gtgt A 13 24 57 gtgt cumsum(A) ans
1 2 3 3 5 7 8
11 14
8
Matrix transpose
EECS 1541 -- Introduction to Computing for the
Physical Sciences
  • If A is an m x n matrix, then the transpose of A
    is an n x m matrix, where the row vectors of A
    are written as column vectors
  • Example

gtgt A 1 2 3 4 5 6 A gtgt
transpose_A A transpose_A
1 2 3 4 5 6
1 3 5 2 4 6
9
Scalar multiplication
EECS 1541 -- Introduction to Computing for the
Physical Sciences
  • Numerical or arithmetic operations can be
    performed on every element in the entire vectors
    or matrices
  • Example of a vector

gtgt v 1 3 5 7 gtgt v3 ans
3 9 15 21
10
Scalar multiplication
EECS 1541 -- Introduction to Computing for the
Physical Sciences
  • Example of a matrix

gtgt A 1 3 5 7 2 4 gtgt A3 ans
3 9 15 21 6 12
11
Scalar addition and subtraction
EECS 1541 -- Introduction to Computing for the
Physical Sciences
  • Example

gtgt A 1 3 5 7 2 4 gtgt A 2 ans
Add every element by 2
3 5 7 9 4 6
12
Array operations addition and subtraction
EECS 1541 -- Introduction to Computing for the
Physical Sciences
  • You can perform element-by-element arithmetic
    with two arrays of the same size
  • Example

gtgt v1 1 2 3 gtgt v2 4 5 6 gtgt v1
v2 ans 5 7 9
Adding two vectors
13
Array operations addition and subtraction
EECS 1541 -- Introduction to Computing for the
Physical Sciences
  • Example

gtgt A1 1 2 3 2 2 5 gtgt A2 1 0 1 3
6 1 gtgt A1 A2 ans 2 2 4 5
8 6
Adding two matrices
14
Array operations addition and subtraction
EECS 1541 -- Introduction to Computing for the
Physical Sciences
  • Example

gtgt A1 1 2 3 2 2 5 gtgt A2 1 0 1 3
6 1 gtgt A1 - A2 ans 0 2 2 -1
-4 4
Subtracting two matrices
15
Array operations addition and subtraction
EECS 1541 -- Introduction to Computing for the
Physical Sciences
  • Example

gtgt A1 1 2 3 2 2 5 gtgt A2 1 0 1 3
6 1 gtgt A2(2,) gtgt A1 - A2 ans
Error using - Matrix dimensions must
agree.
Subtracting two matrices with different dimensions
16
Matrix Multiplication
EECS 1541 -- Introduction to Computing for the
Physical Sciences
  • Multiplication between two matrices works as
    follows
  • To multiply a matrix A by a matrix B to result in
    a matrix C, the number of columns of A must equal
    to the number of row in B

the inner dimensions must be the same
  • Example

2 x 2
2 x 1
2 x 1
17
Matrix Multiplication
EECS 1541 -- Introduction to Computing for the
Physical Sciences
  • Example

gtgt A 1 2 3 2 gtgt B 4 1 gtgt AB ans
6 14
Multiplying two matrices
18
Vector Multiplication
EECS 1541 -- Introduction to Computing for the
Physical Sciences
  • To multiple 2 vectors, they must have the same
    number of elements, but one must be a row vector
    and the other a column vector
  • OR
  • Example

1 x 3
3 x 1
1 x 1
19
Vector Multiplication
EECS 1541 -- Introduction to Computing for the
Physical Sciences
  • Example

1 x 3 vector
gtgt A 1 2 4 gtgt B 2 0 1 gtgt AB ans
6
3 x 1 vector
Multiplying two vectors
20
Vector Multiplication
EECS 1541 -- Introduction to Computing for the
Physical Sciences
  • Example

1 x 3 vector
gtgt A 1 2 4 gtgt B 2 0 1 gtgt BA ans
2 4 8 0 0 0 1 2 4

3 x 1 vector
Multiplying two vectors
3 x 3 matrix
21
Array operations Multiplication
EECS 1541 -- Introduction to Computing for the
Physical Sciences
  • How do we perform element-by-element
    multiplication or division between 2 vectors or 2
    matrices?
  • Example

gtgt v1 16 v1 1 2 3 4 5 6
gtgt v1v1
MATLAB will interpret this one as multiplying a 1
x 6 vector with a 1 x 6 vector
gtgt Error using Inner matrix dimensions must
agree.
22
Array operations Multiplication
EECS 1541 -- Introduction to Computing for the
Physical Sciences
  • To perform element-by-element multiplication-based
    operation between multiple vectors, a dot must
    be placed in front of the operator

operators descriptions Examples in vectors
. element-by-element multiplication gtgt v 1 2 3 4 gtgt v.v ans 1 4 9 16
./ element-by-element division gtgt v 1 2 3 4 gtgt v./v ans 1 1 1 1
. element-by-element exponentiation gtgt v 1 2 3 4 gtgt v.3 ans 1 8 27 64
23
Array operations Multiplication
EECS 1541 -- Introduction to Computing for the
Physical Sciences
  • Recall from LAB 2, when we plot a function that
    involves multiplication among sub-functions, we
    are performing element-by-element multiplication
    operation between multiple vectors
  • Example, to plot the function y xex

A row vector for the independent variable
gtgt x 00.10.5 x 0.0 0.1 0.2
0.3 0.4 0.5 gtgt exp(x) ans
1.000 1.1052 1.2214 1.3499 1.4918
1.6487 gtgt y x.exp(x) y 0.000 0.1105
0.24438 0.4050 0.5967 0.8244
24
Array operations Multiplication
EECS 1541 -- Introduction to Computing for the
Physical Sciences
25
Array operations Multiplication
EECS 1541 -- Introduction to Computing for the
Physical Sciences
  • To perform element-by-element multiplication-based
    operation between 2 matrices, the dimensions
    must be the same

Multiply each element in the same position of A
and B
gtgt A 1 2 3 2 gtgt B 2 0 1 4 gtgt
A.B ans
2 0 3 8
Final result is
26
Array operations Multiplication
EECS 1541 -- Introduction to Computing for the
Physical Sciences
  • NOTE the difference between element-by-element
    multiplication and the actual matrix
    multiplication in this example since we have two
    2 x 2 matrices

gtgt A 1 2 3 2 gtgt B 2 0 1 4 gtgt
A.B ans
gtgt A 1 2 3 2 gtgt B 2 0 1 4 gtgt
AB ans
Matrix multiplication
Element-by-element multiplication
2 0 3 8
4 8 8 8
27
Array operations Multiplication
EECS 1541 -- Introduction to Computing for the
Physical Sciences
gtgt A 1 2 3 2 gtgt B 2 0 1 4 gtgt
B.A ans
gtgt A 1 2 3 2 gtgt B 2 0 1 4 gtgt
BA ans
Matrix multiplication
Element-by-element multiplication
2 0 3 8
2 4 13 10
A.B B.A
AB ? BA
28
Array operations Multiplication
EECS 1541 -- Introduction to Computing for the
Physical Sciences
  • Example

gtgt v 228 gtgt v.2/2 ans
has higher precedence than /
2 8 18 32
  • Example

gtgt A ones(1,3) 13 gtgt A.2 ans
1 1 1 4 1 9
Write a Comment
User Comments (0)
About PowerShow.com