Professional Documents
Culture Documents
ENGR 112 - Chapter9
ENGR 112 - Chapter9
9.1 Operators
The syntax for using operators on symbolic objects is similar to that used for operating on
numeric objects.
©zyBooks 06/05/19 13:47 471670
Berkeley Skuratowicz
Arithmetic Operators OREGONSTATEENGR112AlAbdrabbuhSpring2019
MATLAB®
Description
operator
Matrix addition adds A and B. A and B must have the same dimensions,
A+B
unless one is a scalar.
Matrix left division solves the symbolic linear equations A*X=B for X.
Note that A\B is roughly equivalent to inv(A)*B. Warning messages are
A\B produced if X does not exist or is not unique. Rectangular matrices A are
allowed, but the equations must be consistent; a least squares solution is
not computed for symbolic array variables.
Array left division is the matrix with entries B(i,j)/A(i,j). A and B must have
A.\B
the same dimensions, unless one is a scalar. ©zyBooks 06/05/19 13:47 471670
Berkeley Skuratowicz
Matrix right division solves the symbolic linear equation X*A=B for X.
OREGONSTATEENGR112AlAbdrabbuhSpring2019
Note that B/A is the same as (A.'\B.').'. Warning messages are produced if
B/A X does not exist or is not unique. Rectangular matrices A are allowed, but
the equations must be consistent; a least squares solution is not
computed.
Array right division is the matrix with entries A(i,j)/B(i,j). A and B must
A./B
have the same dimensions, unless one is scalar.
https://learn.zybooks.com/zybook/OREGONSTATEENGR112AlAbdrabbuhSpring2019/chapter/9/print 1/29
6/5/2019 ENGR 112: Introduction to Engineering Computing home
,
A^B Matrix power raises the square matrix A to the integer power B. If A is a
scalar and B is a square matrix, A^B raises A to the matrix power B, using
eigenvalues and eigenvectors. A^B, where A and B are both matrices, is
an error.
p1 =
x^2 + a
syms x y a b p2 =
p1 = x^2+a y^3 + b
p2 = y^3+b p3 =
p3 = p1+p2 x^2 + y^3 + a + b
p4 = p1*p2 p4 =
p4 = expand(p4) (x^2 + a)*(y^3 + b)
p4 =
x^2*y^3 + b*x^2 + a*y^3 + a*b
Relational Operators
equivalent to A > B.
The following example employs the relational equality operator and the function isequaln.
syms x y h =
f = x^2 - 1; x^2 - 1 == y^2 - 1
g = y^2 - 1; fequalsg =
h = f == g 0
fequalsg = isequaln(f,g) fequalsf =
fequalsf = isequaln(f,f) 1
Note that using the "==" operator with symbolic arguments returns a symbolic expression, not a
logical value. The isequaln function is used to compare if two symbolic expressions are equal, as
it returns a logical result. As shown above, even if two symbolic expressions are mathematically
equivalent as f and g are, isequaln() returns false if they are expressed in terms of different
©zyBooks 06/05/19 13:47 471670
symbolic variables. Berkeley Skuratowicz
OREGONSTATEENGR112AlAbdrabbuhSpring2019
lhs(reln) Returns the left hand side of the symbolic relation reln.
https://learn.zybooks.com/zybook/OREGONSTATEENGR112AlAbdrabbuhSpring2019/chapter/9/print 3/29
6/5/2019 ENGR 112: Introduction to Engineering Computing home
rhs(reln) Returns the right hand side of the symbolic relation reln.
pythagoreanThm =
a^2 + b^2 == c^2
syms a b c dx dp hbar heisenberg =
pythagoreanThm = a^2 + b^2 == c^2 hbar/2 <= dx*dp
heisenberg = hbar/2 <= dx*dp pythagoreanLHS =
pythagoreanLHS = lhs(pythagoreanThm) a^2 + b^2
heisenbergLHS = lhs(heisenberg) heisenbergLHS =
heisenbergRHS = rhs(heisenberg) hbar/2
heisenbergRHS =
dx*dp
Logical operators
The following example shows how logical operators can be combined with relational operators
to construct symbolic expressions.
https://learn.zybooks.com/zybook/OREGONSTATEENGR112AlAbdrabbuhSpring2019/chapter/9/print 5/29
6/5/2019 ENGR 112: Introduction to Engineering Computing home
syms x y
relExp1 = x == y x == y
relExp2 = x > y y < x
logExp1 = exp1 & exp2 x == y and y < x ©zyBooks 06/05/19 13:47 471670
Berkeley Skuratowicz
OREGONSTATEENGR112AlAbdrabbuhSpring2019
Note that in the symbolic logical expression, the & operator is printed as the word "and".
Functions that manipulate and simplify symbolic arithmetic expressions also apply to symbolic
logical expressions.
syms x y z
simplify(x & x) x
simplify(x & ~x) FALSE
simplify(x | ~x) TRUE
simplify(~(~(x | y) & ~(y | z))) x or y or z
Note that the symbolic logical constants for true and false print as their respective words.
PARTICIPATION
ACTIVITY
9.1.1: Symbolic operators.
Given the following code, match the result returned from evaluating each statement or
function call.
syms x y z
aExp1 = x + y;
aExp2 = x^2 - z^2;
rExp1 = x == z;
rExp2 = z > aExp1;
©zyBooks 06/05/19 13:47 471670
Berkeley Skuratowicz
OREGONSTATEENGR112AlAbdrabbuhSpring2019
aExp2/aExp1 factor(aExp2) collect(aExp1*aExp2) xor(rExp1,rExp2)
x == z xor x + y < z
(x - z)*(x + z)
https://learn.zybooks.com/zybook/OREGONSTATEENGR112AlAbdrabbuhSpring2019/chapter/9/print 6/29
6/5/2019 ENGR 112: Introduction to Engineering Computing home
(x^2 - z^2)/(x + y)
Reset
©zyBooks 06/05/19 13:47 471670
Berkeley Skuratowicz
OREGONSTATEENGR112AlAbdrabbuhSpring2019
9.2 Sorting
Sorting is the process of converting a list of items into ascending (or descending) order. For
example, given a list of numbers (17, 3, 44, 6, 9), the list after sorting is (3, 6, 9, 17, 44). Sorting is
used in everyday life to, for example, arrange papers in alphabetical order, arrange envelopes to
have ascending ZIP codes, and so on. Using a program to sort introduces a challenge because
each statement in the program can't "see" the entire list to know where to move an item.
Typically, programs are instead limited to observing or comparing only two items at a time, and
then swapping those items. Sorting just by swapping values is an important part of sorting
algorithms. To gain experience with such algorithms, complete the following sorting activity by
swapping elements.
PARTICIPATION
ACTIVITY
9.2.1: Sort the array from smallest on the left to largest on the right.
Sort the numbers from smallest on the left to largest on the right. Repeatedly select
two numbers and click "Swap values". Try again to get your best time.
Start
X X X X X X X
©zyBooks 06/05/19 13:47 471670
Swap
Berkeley Skuratowicz
OREGONSTATEENGR112AlAbdrabbuhSpring2019
Because sorting is such a fundamentally useful tool for processing many kinds of data,
MATLAB® provides some MATLAB functions that implement sorting:
https://learn.zybooks.com/zybook/OREGONSTATEENGR112AlAbdrabbuhSpring2019/chapter/9/print 7/29
6/5/2019 ENGR 112: Introduction to Engineering Computing home
sort
rowEx =
76 75 40 66 18
sortedRowEx =
18 40 66 75©zyBooks
76 06/05/19 13:47 471670
colEx = Berkeley Skuratowicz
rowEx = [ 76, 75, 40, 66, 18 ] 71 OREGONSTATEENGR112AlAbdrabbuhSpring2019
sortedRowEx = sort(rowEx) 4
28
colEx = [ 71; 4; 28; 5; 10 ] 5
sortedColEx = sort(colEx) 10
sortedColEx =
4
5
10
28
71
https://learn.zybooks.com/zybook/OREGONSTATEENGR112AlAbdrabbuhSpring2019/chapter/9/print 8/29
6/5/2019 ENGR 112: Introduction to Engineering Computing home
sortrows
The function sortrows sorts the elements in a speci ed column in an array. All the rows in the
array are moved along with the sorted column elements. Note, the name of the function can be
misleading: The elements in a column are sorted, not the rows.
sampleMatrix =
6 10 7
2 8 1
sampleMatrix = [ 6, 10, 7; ... 5 10 9
2, 8, 1; ... sortSampCol1 =
5, 10, 9 ] 2 8 1
sortSampCol1 = sortrows(sampleMatrix,1) 5 10 9
sortSampCol2 = sortrows(sampleMatrix,2) 6 10 7
sortSampCol2 =
©zyBooks 06/05/19 13:47 471670
2 8 1
Berkeley Skuratowicz
6 10 7
OREGONSTATEENGR112AlAbdrabbuhSpring2019
5 10 9
In the second call to sortrows in the above example, rows in the second column have equal
elements (both have value 10). In this case, the sorting is based on the column immediately to
the right (i.e., the third column of sampleMatrix).
https://learn.zybooks.com/zybook/OREGONSTATEENGR112AlAbdrabbuhSpring2019/chapter/9/print 9/29
6/5/2019 ENGR 112: Introduction to Engineering Computing home
PARTICIPATION
ACTIVITY
9.2.2: sort and sortrows.
PARTICIPATION
ACTIVITY
9.2.3: Using sort and sortrows.
8, 5, 14
matrixA = 9, 6, 11
9, 3, 2
Answer eld
Sorted indices
The sorted indices returned by the sort and sortrows functions are the indices of the sorted
array elements in the original array:
In the following gure, an input array is sorted, with a sorted array as the output.
However, the sorted indexes are also returned, and they point to the original positions in
the input array.
Meaning of sorted indices when a sort is performed.
©zyBooks 06/05/19 13:47 471670
Berkeley Skuratowicz
OREGONSTATEENGR112AlAbdrabbuhSpring2019
https://learn.zybooks.com/zybook/OREGONSTATEENGR112AlAbdrabbuhSpring2019/chapter/9/print 11/29
6/5/2019 ENGR 112: Introduction to Engineering Computing home
rowEx =
76 75 40 66 18
sortedRowEx =
18 40 66 75 76
sortedRowIdx =
5 3 4 2 1
©zyBooks 06/05/19 13:47 471670
colEx = Berkeley Skuratowicz
71
OREGONSTATEENGR112AlAbdrabbuhSpring2019
4
rowEx = [ 76, 75, 40, 66, 18 ] 28
[ sortedRowEx, sortedRowIdx ] = sort(rowEx) 5
10
colEx = [ 71; 4; 28; 5; 10 ] sortedColEx =
[ sortedColEx, sortedColIdx ] = sort(colEx) 4
5
10
28
71
sortedColIdx =
2
4
5
3
1
In the above example, sortedRowEx has value 18 as its rst element (index 1). In the original
array rowEx, 18 was the last element (index 5). This index is the rst element in sortedRowIdx,
corresponding to the index of the value 18 in rowEx. The second element in sortedRowEx is 40,
which had an index of 3 in rowEx. Consequently, the second element in sortedRowIdx is 3.
Another way to look at the returned sorted indices is that the statements
sortedRowEx = sort(rowEx) and sortedRowEx = rowEx(sortedRowIdx) produce the
same array. The sorted indices for the column array sortedColIdx can be thought of in the same
way.
A common use of sorted indices is to reference data in an array based on sorting data
in another array. In the following example, the sortrows function will be used to display
information retrieved from a grocery database represented ©zyBooks
by two06/05/19
arrays. 13:47 471670
Berkeley Skuratowicz
OREGONSTATEENGR112AlAbdrabbuhSpring2019
The following table captures the type, cost, and quantity of fruit ordered by a grocery
store manager:
Apples $2 10
Apricots $3 100
https://learn.zybooks.com/zybook/OREGONSTATEENGR112AlAbdrabbuhSpring2019/chapter/9/print 12/29
6/5/2019 ENGR 112: Introduction to Engineering Computing home
Grapes $3.50 30
Pears $2.40 40
The numerical data is placed in a numeric array priceData. The character vectors
©zyBooks 06/05/19 13:47 471670
describing fruit types are placed in a separate array dataLabels:
Berkeley Skuratowicz
OREGONSTATEENGR112AlAbdrabbuhSpring2019
Creating arrays to represent the grocery data.
% Grocery data
priceData = [[2;3;3.50;2.50],[10;100;30;40]]
dataLabels = char('Apples',...
'Apricots','Grapes','Pears');
[ numItems, numCats ] = size(priceData);
The numeric and character types cannot be placed in the same array, so two arrays are
used. In another section, structure variables and cell arrays will be discussed, which
allow mixing of data types to create a coherent database that only needs one variable.
numItems is the number of different items tracked by the table and numCats is the
number of categories of numeric information, corresponding to the rows and columns
of the priceData array, respectively.
The code below shows how the data is rst sorted by price; then, the sorted indices are
used to display the items in sorted order:
Sort the data by price and display items in sorted order.
% Sort items by increasing price
[ itemsSortPrice, sortPriceIdx ] = sortrows(priceData,1);
sortPriceIdx % sorted indices by increasing price
disp('The items in order of increasing price are:')
disp(dataLabels(sortPriceIdx,:)) % single colon references
sortPriceIdx =
1
4
2
3
https://learn.zybooks.com/zybook/OREGONSTATEENGR112AlAbdrabbuhSpring2019/chapter/9/print 13/29
6/5/2019 ENGR 112: Introduction to Engineering Computing home
sortQtyIdx =
©zyBooks 06/05/19 13:47 471670
1 Berkeley Skuratowicz
3 OREGONSTATEENGR112AlAbdrabbuhSpring2019
4
2
The items in order of increasing quantity are:
Apples
Grapes
Pears
Apricots
PARTICIPATION
ACTIVITY
9.2.4: Sorting.
Given:
rowEx = randi(100,1,10);
[ sortRowEx,indexSeq ] = sort(rowEx);
thirdSmallest =
Answer eld ;
https://learn.zybooks.com/zybook/OREGONSTATEENGR112AlAbdrabbuhSpring2019/chapter/9/print 14/29
6/5/2019 ENGR 112: Introduction to Engineering Computing home
thirdLargest =
Answer eld ;
For the array on the left, what command will produce the array sorted by the second
column as shown on the right?
19216801, 1, 3 19216801, 1, 3
19216802, 5, 23 19216801, 2, 2
sampleMatrix 19216802, 3, 45 sortedMatrix 19216802, 3, 45
= 19216801, 2, 2 = 19216801, 4, 3
19216801, 4, 3 19216802, 5, 23
19216802, 7, 5 [19216802, 7, 5
sampleMatrix=[....
[19216801, 1, 3];...
[19216802, 5, 23];...
[19216802, 3, 45];...
[19216801, 2, 2];...
©zyBooks 06/05/19 13:47 471670
[19216801, 4, 3];... Berkeley Skuratowicz
[19216802, 7, 5];... OREGONSTATEENGR112AlAbdrabbuhSpring2019
]
sortedMatrix =
Answer eld
;
https://learn.zybooks.com/zybook/OREGONSTATEENGR112AlAbdrabbuhSpring2019/chapter/9/print 15/29
6/5/2019 ENGR 112: Introduction to Engineering Computing home
CHALLENGE This tool is provided by a third party. Though your activity may be
ACTIVITY
9.2.1: Row sort. recorded, a page refresh may be needed to ll the banner.
https://learn.zybooks.com/zybook/OREGONSTATEENGR112AlAbdrabbuhSpring2019/chapter/9/print 16/29
6/5/2019 ENGR 112: Introduction to Engineering Computing home
CHALLENGE This tool is provided by a third party. Though your activity may be
ACTIVITY
9.2.2: Dizzy Shift. recorded, a page refresh may be needed to ll the banner.
https://learn.zybooks.com/zybook/OREGONSTATEENGR112AlAbdrabbuhSpring2019/chapter/9/print 17/29
6/5/2019 ENGR 112: Introduction to Engineering Computing home
Matrix addition/subtraction
A vector can be viewed as a subset of a matrix, and therefore the addition and scalar
multiplication vector operations extend to matrices. Matrices can be added or subtracted if the
matrices have the same number of rows and columns. Adding/subtracting the matrices A and
B is
matA =
1 2
3 4
matB =
matA = [1, 2; 3, 4] 5 6
matB = [5, 6; 7, 8] 7 8
sumMat = matA + matB sumMat =
subMat = matA - matB 6 8
10 12
subMat =
-4 -4
-4 -4
©zyBooks 06/05/19 13:47 471670
Berkeley Skuratowicz
OREGONSTATEENGR112AlAbdrabbuhSpring2019
PARTICIPATION
ACTIVITY
9.3.1: Matrix addition.
False
⎝ ⎠
αam1 αam2 ⋯ αamn
Matrix multiplication
Two matrices, A and B , can be multiplied if the number of columns of the rst matrix A is the
same as the number of rows of the second matrix B . Stated differently, the product of an
(i × j) matrix, A , with a (k × l) matrix, B , exists if the inner integers j and k are equal. The
⎝ ⎠⎝ ⎠
am1 am2 ⋯ amn bm1 bm2 ⋯ bmk
Matrix multiplication is not commutative. Assuming the products AB and BA are de ned (i.e.
both A and B are square and of the same size), AB generally does not equal BA.
https://learn.zybooks.com/zybook/OREGONSTATEENGR112AlAbdrabbuhSpring2019/chapter/9/print 19/29
6/5/2019 ENGR 112: Introduction to Engineering Computing home
PARTICIPATION
ACTIVITY
9.3.2: Multiplication of a row with col vector, and vice versa.
Animation captions:
1. The rst row element in the rst array is multiplied with each element in the second
array. The process is repeated with the second row element in the rst array.
©zyBooks 06/05/19 13:47 471670
2. The result is a 2x2 array. Berkeley Skuratowicz
3. The product of the rst array (1,1) element OREGONSTATEENGR112AlAbdrabbuhSpring2019
with the second array (1,1) element is added
to the product of the rst array (1,2) element with the second array (2,1) element.
4. The result is a 1x1 array.
PARTICIPATION
ACTIVITY
9.3.3: Multiplication of 2D array by a 2D array.
Animation captions:
1. The m-th row elements in the rst matrix are multiplied with the n-th column elements in
the second matrix and the products are added.
2. The result is the product matrix.
PARTICIPATION
ACTIVITY
9.3.4: Multiplying matrices: Important rule.
False
https://learn.zybooks.com/zybook/OREGONSTATEENGR112AlAbdrabbuhSpring2019/chapter/9/print 20/29
6/5/2019 ENGR 112: Introduction to Engineering Computing home
True
False
PARTICIPATION
ACTIVITY
9.3.5: Length of vector.
Answer eld
Answer eld
⎝ ⎠ ⎝ ⎠ ⎝ ⎠
am1 am2 ⋯ amn bn am1 b1 + am2 b2 + ⋯ + amn bn
PARTICIPATION
ACTIVITY
9.3.6: Method 1: Multiplying a matrix with©zyBooks
a column06/05/19
vector. 13:47 471670
Berkeley Skuratowicz
OREGONSTATEENGR112AlAbdrabbuhSpring2019
Animation captions:
1. Calculate the inner product of the matrix's top row with the column vector.
2. Calculate the inner product of the matrix's bottom row with the column vector.
3. The new matrix is a 2 x 1 column vector.
https://learn.zybooks.com/zybook/OREGONSTATEENGR112AlAbdrabbuhSpring2019/chapter/9/print 21/29
6/5/2019 ENGR 112: Introduction to Engineering Computing home
Each resultant column entry j is the inner product (a.k.a. scalar product) between the j-th row
vector of A and b :
T
(a a12 ⋯ a1n ) b1 (a a12 ⋯ a1n ) ⋅ b
⎛ 11 ⎞ ⎛ ⎞ ⎛ 11 ⎞
T
(a a2n ) ⎟ ⎜ b2 ⎜ (a ⎟
⎜ 21 a22 ⋯ ⎟ 21 a22 ⋯ a2n ) ⋅ b
⎜ ⎟ ⎜ ⎟ ⎜ ⎟
Ab = = ⎜ ⎟
⎜ ⎟ ⎜ ⎟
⎜ ⎟
⎜ ⋮ ⎟ ⎜ ⋮ ⎟
⎜ ⋮ ⎟
⎝ ⎠
©zyBooks 06/05/19 13:47 471670
⎝ ⎠ ⎝ T ⎠
(a
m1 am2 ⋯ amn ) bm ( aBerkeley
am2Skuratowicz
⋯ amn ) ⋅ b
m1
OREGONSTATEENGR112AlAbdrabbuhSpring2019
In linear algebra, a set of linear equations can be written as Ax = b where the entries of A an
constants and x are the unknowns. The relationship Ax = b can viewed as the inner product
row vectors of A with x:
T
(a a12 ⋯ a1n ) x1 (a a12 ⋯ a1n ) ⋅ x
⎛ 11 ⎞ ⎛ ⎞ ⎛ 11 ⎞
T
⎜ (a a22 ⋯ a2n ) ⎟ ⎜ x2 ⎟ ⎜ (a a22 ⋯ a2n ) ⋅ x
21 21
⎜ ⎟ ⎜ ⎟ ⎜
Ax = = ⎜
⎜ ⎟ ⎜ ⎟
⎜
⎜ ⋮ ⎟ ⎜ ⋮ ⎟
⎜ ⋮
⎝ ⎠ ⎝ ⎠ ⎝ ⎠
T
(a am2 ⋯ amn ) xm (a am2 ⋯ amn ) ⋅ x
m1 m1
This shows that Ab can viewed as the sum of the column vectors of A where each vector is
multiplied by the weights of the corresponding elements of b . The two different views are
important in solving linear set of equations. ©zyBooks 06/05/19 13:47 471670
Berkeley Skuratowicz
OREGONSTATEENGR112AlAbdrabbuhSpring2019
PARTICIPATION
ACTIVITY
9.3.7: Method 2: Multiplying a matrix with a column vector.
Animation captions:
https://learn.zybooks.com/zybook/OREGONSTATEENGR112AlAbdrabbuhSpring2019/chapter/9/print 22/29
6/5/2019 ENGR 112: Introduction to Engineering Computing home
1. Form the sum of column vectors of the matrix multiplied by the weights of the vector
elements.
2. Simplify.
3. Simplify.
4. Solve to get the resulting matrix.
A set of linear equations Ax = b can viewed as the set of equations where the column vectors
of A are weighted by the entries of x:
⎝ ⎠ ⎝ ⎠ ⎝ ⎠
am1 am2 amn
PARTICIPATION
ACTIVITY
9.3.8: Valid matrix-matrix operations.
https://learn.zybooks.com/zybook/OREGONSTATEENGR112AlAbdrabbuhSpring2019/chapter/9/print 23/29
6/5/2019 ENGR 112: Introduction to Engineering Computing home
rowA =
1 2 3
colB =
2
4
6
rowA = [1, 2, 3] multRowCol =
colB = [2; 4; 6] 28
multRowCol = rowA * colB % Matrix multiplication colA =
colA = rowA' 1
rowB = colB' 2
©zyBooks 06/05/19 13:47 471670
multColRow = colA * rowB % Matrix multiplication 3 Berkeley Skuratowicz
rowB =
intentError = rowA * rowB % Intentional errorOREGONSTATEENGR112AlAbdrabbuhSpring2019
2 4 6
multColRow =
2 4 6
4 8 12
6 12 18
Error using *
Inner matrix dimensions must agree.
matA =
1 2 3
4 5 6
matA = [1, 2, 3; 4, 5, 6] matB =
matB = [1, 1, 1; 2, 2, 2; 3 , 3, 3] 1 1 1
matAmultmatBCol = matA * matB % Matrix multiplication 2 2 2
3 3 3
matAmultmatBCol =
14 14 14
32 32 32
Since
©zyBooks 06/05/19 13:47 471670
Ab = b1 a1 + b2 a2 + ⋯ + bBerkeley
n an Skuratowicz
OREGONSTATEENGR112AlAbdrabbuhSpring2019
if b only contains ones, the sum of each row in the matrix A is calculated:
https://learn.zybooks.com/zybook/OREGONSTATEENGR112AlAbdrabbuhSpring2019/chapter/9/print 24/29
6/5/2019 ENGR 112: Introduction to Engineering Computing home
A =
1 3 3 2 2
2 2 3 4 3
4 5 5 4 1
b =
m = 3; n = 5; 1
A = randi(5,m,n) 1
b = ones(n,1) 1
A * b 1
1
ans = ©zyBooks 06/05/19 13:47 471670
11 Berkeley Skuratowicz
14
OREGONSTATEENGR112AlAbdrabbuhSpring2019
19
⎝ ⎝ ⎠ ⎝ ⎠ ⎝ ⎠ ⎠
bm1 bm2 bmk
2. Row vector-matrix: Each row of AB is the product with the corresponding row vector of A
AB equals row vector i of A times B :
https://learn.zybooks.com/zybook/OREGONSTATEENGR112AlAbdrabbuhSpring2019/chapter/9/print 25/29
6/5/2019 ENGR 112: Introduction to Engineering Computing home
⎝ ⎠ ⎝⎝ ⎠⎝ ⎠
(a am2 ⋯ amn ) bm1 bm2
m1
4. Column vector with row vector: The matrix product equals the sum of the products of the
©zyBooks 06/05/19 13:47 471670
vectors of B : Berkeley Skuratowicz
OREGONSTATEENGR112AlAbdrabbuhSpring2019
a11 a12 a1n ( b11 b12
⎛⎛ ⎞⎛ ⎞ ⎛ ⎞ ⎞⎛
a11 a12
⎛ ⎞ ⎛ ⎞
⎜ a21 ⎟ ⎜ a22 ⎟
⎜ ⎟ ⎜ ⎟
AB = ( ) + ( b2n ) + ⋯
⎜ ⎟ b11 b12 ⋯ b1n ⎜ ⎟ b21 b22 ⋯
⎜ ⎟ ⎜ ⎟
⋮ ⋮
⎝ ⎠ ⎝ ⎠
am1 am2
matA =
6 1
7 6
5 7
matB =
matA = randi(7,3,3) 3 5
matB = randi(7,3,3) 5 1
% Typical matrix multiplication in MATLAB 2 2
matCheck = matA * matB matCheck =
33 41
% Alternate viewpoints 63 53
% 1: Matrix-column vector calculation 62 44
mat1 = [matA*matB(:,1), matA * matB(:,2), matA * matB(:,3)] mat1 =
% 2: Row vector-matrix calculation 33 41
mat2 = [matA(1,:) * matB; matA(2,:) * matB; matA(3,:) * matB ] 63 53
% 3: Scalar/Inner/Dot product calculation 62 44
mat3 = [... mat2 =
dot(matA(1,:),matB(:,1)) dot(matA(1,:),matB(:,2)) dot(matA(1,:),matB(:,3));... 33 41
dot(matA(2,:),matB(:,1)) dot(matA(2,:),matB(:,2)) dot(matA(2,:),matB(:,3));... 63 53
dot(matA(3,:),matB(:,1)) dot(matA(3,:),matB(:,2)) dot(matA(3,:),matB(:,3))] 62 44
% #4 Column vector with row vector matrix calculation mat3 =
©zyBooks
mat4 = matA(:,1) * matB(1,:) + matA(:,2) * matB(2,:) + matA(:,3) 06/05/19 13:47 471670
* matB(3,:) 33 41
Berkeley Skuratowicz 63 53
62 44
OREGONSTATEENGR112AlAbdrabbuhSpring2019
mat4 =
33 41
63 53
62 44
PARTICIPATION
https://learn.zybooks.com/zybook/OREGONSTATEENGR112AlAbdrabbuhSpring2019/chapter/9/print 26/29
6/5/2019 ENGR 112: Introduction to Engineering Computing home
PARTICIPATION
ACTIVITY 9.3.9: Matrix multiplication.
If rowA is a row array with n elements and colB is a column array with n elements.
rowA*colB' rowA*colB
©zyBooks 06/05/19 13:47 471670
Berkeley Skuratowicz
OREGONSTATEENGR112AlAbdrabbuhSpring2019
Valid operation.
Generates an error.
Reset
PARTICIPATION
ACTIVITY
9.3.10: Matrix arithmetic operations using 1D arrays.
In the United States, the Corporate Average Fuel Economy (CAFE) regulations must be
satis ed by manufacturers of cars and light trucks. The CAFE rating is expressed as
miles per gallon (mpg) and is calculated by the equation below:
N
∑ ni
i=1
CAFE =
N
∑ (n i /ci )
i=1
where ni is the number of vehicles in a category and ci is the fuel economy (e.g.
expressed in miles/gallon) in the i -th vehicle category. The quantity ni /ci calculates
the gallons/miles of the i -th vehicles category, and ∑N
i=1
(n i /ci ) calculates the total
https://learn.zybooks.com/zybook/OREGONSTATEENGR112AlAbdrabbuhSpring2019/chapter/9/print 27/29
6/5/2019 ENGR 112: Introduction to Engineering Computing home
wAgMPG =
Answer eld
totVehicles =
Answer eld
CAFE =
Answer eld
PARTICIPATION
ACTIVITY
9.3.11: Matrix multiplication.
1 5 3
xMat = ( )
3 2 2
coded in Matlab as
xMat = [1, 5, 3; 3, 2, 2]
https://learn.zybooks.com/zybook/OREGONSTATEENGR112AlAbdrabbuhSpring2019/chapter/9/print 28/29
6/5/2019 ENGR 112: Introduction to Engineering Computing home
yMat = [ 4, 1; 2, 3; 5, 2]
answer the questions below by writing MATLAB code.
https://learn.zybooks.com/zybook/OREGONSTATEENGR112AlAbdrabbuhSpring2019/chapter/9/print 29/29