Download as pdf or txt
Download as pdf or txt
You are on page 1of 29

6/5/2019 ENGR 112: Introduction to Engineering Computing home

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

Table 9.1.1: Arithmetic operators.

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 subtraction subtracts B from A. A and B must have the same


A-B
dimensions, unless one is a scalar.

Matrix multiplication is the linear algebraic product of A and B. The


A*B number of columns of A must equal the number of rows of B, unless one
is a scalar.

Array multiplication is the element-by-element product of A and B. A and


A.*B
B must have the same dimensions, unless one is 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.

Array power is the matrix with entries A(i,j)^B(i,j).


©zyBooks A and B must
06/05/19 have
13:47 the
471670
A.^B Berkeley Skuratowicz
same dimensions, unless one is scalar.
OREGONSTATEENGR112AlAbdrabbuhSpring2019

Matrix Hermitian transpose. If A is complex, A' is the complex conjugate


A'
transpose.

Array transpose is the real transpose of A. A.' does not conjugate


A.'
complex entries.

Arithmetic operators can be used to create symbolic expressions.

Figure 9.1.1: Using arithmetic operators with


symbolic objects.

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

Table 9.1.2: Relational operators. ©zyBooks 06/05/19 13:47 471670


Berkeley Skuratowicz
OREGONSTATEENGR112AlAbdrabbuhSpring2019

MATLAB operator Description

Creates a symbolic equation. eq(A,B) is equivalent to A


A == B
== B.

Creates a greater than or equal to symbolic relation.


A >= B
ge(A,B) is equivalent to A >= B.
https://learn.zybooks.com/zybook/OREGONSTATEENGR112AlAbdrabbuhSpring2019/chapter/9/print 2/29
6/5/2019 ENGR 112: Introduction to Engineering Computing home
ge( , ) s equ a e t to .

A>B Creates a greater than symbolic relation. gt(A,B) is

equivalent to A > B.

Creates a less than or equal to symbolic relation. le(A,B)


A <= B
is equivalent to A <= B.
©zyBooks 06/05/19 13:47 471670
Creates a less than symbolic relation. lt(A,B)
Berkeley is equivalent
Skuratowicz
A<B OREGONSTATEENGR112AlAbdrabbuhSpring2019
to A < B.

Creates an inequality symbolic relation. ne(A,B) is


A ~= B
equivalent to A ~= B.

Test symbolic objects for equality, treating NaN values as


isequaln(A1,A2,...,An) equal. isequaln(A1,A2,...,An) returns logical 1 (true) if all
the inputs are equal.

The following example employs the relational equality operator and the function isequaln.

Figure 9.1.2: Using relational operators with


symbolic objects.

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

Table 9.1.3: Symbolic relation manipulation.

MATLAB operator Description

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.

The following examples use the lhs and rhs functions.

Figure 9.1.3: Using relational expression ©zyBooks 06/05/19 13:47 471670


Berkeley Skuratowicz
manipulation. OREGONSTATEENGR112AlAbdrabbuhSpring2019

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

Table 9.1.4: Logical operators.

MATLAB operator Description

Logical AND for symbolic expressions. A & B represents the


A&B logical conjunction. A & B is true only when both A and B are
true. and(A,B) is equivalent to A & B.

Logical NOT for symbolic expressions. ~A represents the


~A logical negation. ~A is true when A is false and vice versa.
not(A) is equivalent to ~A.

Logical OR for symbolic expressions. A | B represents the


A|B logical disjunction. A | B is true ©zyBooks 06/05/19
when either A or B13:47 471670
or both are
Berkeley Skuratowicz
true. or(A,B) is equivalent to A | B.
OREGONSTATEENGR112AlAbdrabbuhSpring2019

Logical XOR for symbolic expressions. xor(A,B) represents


the logical exclusive disjunction. xor(A,B) is true when either
xor(A,B)
A or B are true. If both A and B are true or false, xor(A,B) is
false.

all(A) Test whether all equations and inequalities represented as


l f b li A
https://learn.zybooks.com/zybook/OREGONSTATEENGR112AlAbdrabbuhSpring2019/chapter/9/print lid ll(A) h h 4/29
6/5/2019 ENGR 112: Introduction to Engineering Computing home
elements of symbolic array A are valid. all(A) tests whether
all elements of A return logical 1 (true). If A is a 2D array, all()

tests all elements of each column. If A is a multidimensional


array, all() tests all elements along one dimension. all(A,dim)
tests along the dimension of A speci ed by dim.

Test whether at least one of equations


©zyBooksand inequalities
06/05/19 13:47 471670
Berkeley Skuratowicz
represented as elements of symbolic array A is valid. any(A)
OREGONSTATEENGR112AlAbdrabbuhSpring2019
tests whether at least one element of A returns logical 1
any(A) (true). If A is a matrix, any tests elements of each column. If
A is a multidimensional array, any tests elements along one
dimension. any(A,dim) tests along the dimension of A
speci ed by dim.

Check whether symbolic array elements are nite. is nite(A)


returns an array of the same size as A containing logical 1s
(true) where the elements of A are nite, and logical 0s
is nite(A)
(false) where they are not. For a complex number, is nite
returns 1 if both the real and imaginary parts of that number
are nite. Otherwise, it returns 0.

Check whether symbolic array elements are in nite. isinf(A)


returns an array of the same size as A containing logical 1s
(true) where the elements of A are in nite, and logical 0s
isinf(A) (false) where they are not. For complex number, isinf returns
1 if the real or imaginary part of that number is in nite or
both real and imaginary parts are in nite. Otherwise, it
returns 0.

Check whether symbolic array elements are NaNs. isnan(A)


returns an array of the same size as A containing logical 1s
isnan(A)
(true) where the elements of A are symbolic NaNs, and
logical 0s (false) where they are not.

logical(cond) checks whether the condition cond is valid.


cond is an equation, inequality, or a 1D or 2D array of
logical(cond) equations or inequalities. You also can combine
©zyBooks 06/05/19 several
13:47 471670
Berkeley Skuratowicz
conditions by using theOREGONSTATEENGR112AlAbdrabbuhSpring2019
logical operators and, or, xor, not, or
their shortcuts.

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

Figure 9.1.4: Using logical operators with


symbolic objects.

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.

Figure 9.1.5: Simplifying 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)

x^3 + y*x^2 - x*z^2 - y*z^2

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

Time - Best time - Clear best

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

Table 9.2.1: Sorting functions.

MATLAB function Description

sort sorts the elements of inArray in ascending


order and returns the result in sortOut. For 2D
©zyBooks
arrays, sort(inArray) 06/05/19
sorts each 13:47of
column 471670
Berkeley Skuratowicz
inArray inOREGONSTATEENGR112AlAbdrabbuhSpring2019
ascending order. To achieve
[sortOut,sortIndices] = sort(inArray) descending order, the function call is
sort(inArray,'descend'). The array sortIndices
holds the sorted indices of the array elements,
which are the row indices of the elements of
sortOut in the original array inArray.

sortrows sorts the array inArray based on the


values in column colRef while keeping the
rows together. For any rows that have equal
elements in a particular column, sorting is
[sortOut,sortIndices] =
based on the column immediately to the right.
sortrows(inArray,colRef)
When called with only a single argument,
sortrows bases the sort on the rst column of
the matrix. The array sortIndices holds the
sorted indices.

sort

For 1D arrays, sort(oneDArray) sorts a row (column) array by columns (rows).

Figure 9.2.1: Sorting 1D arrays.

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

For 2D arrays, sort(matrixA) sorts each column of matrixA in ascending order.

Figure 9.2.2: Sorting 2D arrays.

©zyBooks 06/05/19 13:47 471670


arrayEx1 = Berkeley Skuratowicz
83 96OREGONSTATEENGR112AlAbdrabbuhSpring2019
39
70 4 77
32 44 80
arrayEx1 = [ 83, 96, 39; ... sortedArrayEx1 =
70, 4, 77; ... 32 4 39
32, 44, 80 ] 70 44 77
sortedArrayEx1 = sort(arrayEx1) 83 96 80

arrayEx2 = [ 29, 90, 14, 85; ... arrayEx2 =


51, 96, 15, 26; ... 29 90 14 85
70, 55, 14, 82 ] 51 96 15 26
sortedArrayEx2 = sort(arrayEx2) 70 55 14 82
sortedArrayEx2 =
29 55 14 26
51 90 14 82
70 96 15 85

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.

Figure 9.2.3: sortrows.

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.

Contrasting attributes exist between sort and sortrows.

1) The entries of rows are sorted with


the function sort
©zyBooks 06/05/19 13:47 471670
True
Berkeley Skuratowicz
False OREGONSTATEENGR112AlAbdrabbuhSpring2019

2) In the sort function there is a


dependence between the columns
when the sort operation is
performed.
True
False

3) In sortrows there is a dependence


between the columns when the sort
operation is performed.
True
False

PARTICIPATION
ACTIVITY
9.2.3: Using sort and sortrows.

Given the array

8, 5, 14  
 
matrixA =   9, 6, 11  
 
9, 3, 2  

Answer the following questions. Write arrays as [ 1, 2, 3; 4, 5, 6 ].


©zyBooks 06/05/19 13:47 471670
1) What is the value of sortedMtx after Berkeley Skuratowicz
executing the statement sortedMtx OREGONSTATEENGR112AlAbdrabbuhSpring2019
= sort([ 55, 8, 99, 32 ]);?
Answer eld

Check Show answer

2) What is the value of sortedMtx after


https://learn.zybooks.com/zybook/OREGONSTATEENGR112AlAbdrabbuhSpring2019/chapter/9/print 10/29
6/5/2019 ENGR 112: Introduction to Engineering Computing home

executing the statement sortedMtx


= sort(matrixA);?
Answer eld

Check Show answer

3) What is the value of sortedMtx after ©zyBooks 06/05/19 13:47 471670


executing the statement sortedMtx Berkeley Skuratowicz
OREGONSTATEENGR112AlAbdrabbuhSpring2019
= sortrows(matrixA,3);?

Answer eld

Check Show answer

4) What is the value of sortedMtx after


executing the statement sortedMtx
= sortrows(matrixA);?
Answer eld

Check Show answer

Sorted indices

The sorted indices returned by the sort and sortrows functions are the indices of the sorted
array elements in the original array:

Example 9.2.1: Sorted indices for 1D arrays.

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.

Example 9.2.2: Using sorted indices with 2D arrays.

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:

Type Cost Quantity

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

The items in order of increasing price are:


Apples
Pears
Apricots
©zyBooks 06/05/19 13:47 471670
Grapes
Berkeley Skuratowicz
OREGONSTATEENGR112AlAbdrabbuhSpring2019
To display the list in order of increasing quantity, sortrows are called with the second
column speci ed in the argument list:
Sort the data by quantity and display items in sorted order.

https://learn.zybooks.com/zybook/OREGONSTATEENGR112AlAbdrabbuhSpring2019/chapter/9/print 13/29
6/5/2019 ENGR 112: Introduction to Engineering Computing home

% Sort items by increasing quantity


[ itemsSortQty, sortQtyIdx ] = sortrows(priceData,2);
sortQtyIdx % sorted indices by increasing quantity
disp('The items in order of increasing quantity are:')
disp(dataLabels(sortQtyIdx,:)) % single colon references

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);

1) Using the array sortRowEx,


complete the statement to retrieve
the value of the 3rd smallest
element in rowEx:

thirdSmallest =
Answer eld ;

Check Show answer

2) Using the array indexSeq, complete


the statement to retrieve the 3rd
smallest element in rowEx:
©zyBooks 06/05/19 13:47 471670
Berkeley Skuratowicz
thirdSmallest = OREGONSTATEENGR112AlAbdrabbuhSpring2019
Answer eld ;

Check Show answer

3) Complete the following statement to


retrieve the 3rd largest element in

https://learn.zybooks.com/zybook/OREGONSTATEENGR112AlAbdrabbuhSpring2019/chapter/9/print 14/29
6/5/2019 ENGR 112: Introduction to Engineering Computing home

rowEx. Make use of the 'end'


keyword to index the element.

thirdLargest =
Answer eld ;

Check Show answer


©zyBooks 06/05/19 13:47 471670
Berkeley Skuratowicz
OREGONSTATEENGR112AlAbdrabbuhSpring2019
PARTICIPATION
ACTIVITY
9.2.5: Using sortrows.

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
   

   

1) Complete the command:

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
;

Check Show answer

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.

©zyBooks 06/05/19 13:47 471670


Berkeley Skuratowicz
OREGONSTATEENGR112AlAbdrabbuhSpring2019

©zyBooks 06/05/19 13:47 471670


Berkeley Skuratowicz
OREGONSTATEENGR112AlAbdrabbuhSpring2019

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.

©zyBooks 06/05/19 13:47 471670


Berkeley Skuratowicz
OREGONSTATEENGR112AlAbdrabbuhSpring2019

©zyBooks 06/05/19 13:47 471670


Berkeley Skuratowicz
OREGONSTATEENGR112AlAbdrabbuhSpring2019

https://learn.zybooks.com/zybook/OREGONSTATEENGR112AlAbdrabbuhSpring2019/chapter/9/print 17/29
6/5/2019 ENGR 112: Introduction to Engineering Computing home

9.3 Matrix calculations


©zyBooks 06/05/19 13:47 471670
Berkeley Skuratowicz
OREGONSTATEENGR112AlAbdrabbuhSpring2019
 This section has been set as optional by your instructor.

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

a11 ± b11 a12 ± b12 ⋯ a1n ± b1n


⎛ ⎞

⎜ a21 ± b21 a22 ± b22 ⋯ a1n ± b1n ⎟


⎜ ⎟
A ± B =
⎜ ⎟
⎜ ⎟
⋮ ⋮ ⋮
⎝ ⎠
am1 ± bm1 am2 ± bm2 ⋯ amn ± bmn

Example 9.3.1: Matrix addition/subtraction.

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.

1) Two matrices can be added if both


are (2 × 4) matrices.
True
https://learn.zybooks.com/zybook/OREGONSTATEENGR112AlAbdrabbuhSpring2019/chapter/9/print 18/29
6/5/2019 ENGR 112: Introduction to Engineering Computing home

False

2) Two matrices can be added if the


rst is a (2 × 3) matrix and the
second is a (3 × 2) matrix.
True
False ©zyBooks 06/05/19 13:47 471670
Berkeley Skuratowicz
OREGONSTATEENGR112AlAbdrabbuhSpring2019
Scalar multiplication

Scalar multiplication operates independently on each matrix entry:

αa11 αa12 ⋯ αa1n


⎛ ⎞

⎜ αa21 αa22 ⋯ αa2n ⎟


⎜ ⎟
αA =
⎜ ⎟
⎜ ⋮ ⋮ ⋮ ⎟

⎝ ⎠
α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

matrix multiplication will produce an (i × l) matrix.

The product of an (m × n) matrix A with an (n × k) matrix B exists. The matrix product is a


(m × k) matrix

a11 a12 ⋯ a1n b11 b12 ⋯ b1k


⎛ ⎞⎛ ⎞

⎜ a21 a22 ⋯ a2n ⎟ ⎜ b21 b22 ⋯ b2k ⎟


⎜ ⎟ ⎜ ⎟
AB =
⎜ ⎟ ⎜ ⎟
⎜ ⋮ ⋮ ⋮ ⎟ ⎜ ⋮ ⋮ ⋮ ⎟

⎝ ⎠⎝ ⎠
am1 am2 ⋯ amn bm1 bm2 ⋯ bmk

The product is calculated as


©zyBooks 06/05/19 13:47 471670
a11 b11 + a12 b21 + ⋯ + a1n bm1 a11 b12 +Berkeley
a12 b22 Skuratowicz
+ ⋯ + a1n bm2 ⋯

OREGONSTATEENGR112AlAbdrabbuhSpring2019
⎜ a21 b11 + a22 b22 + ⋯ + a2n bm1 a21 b12 + a22 b22 + ⋯ + a2n bm2 ⋯

AB =

⎜ ⋮ ⋮

am1 b11 + am2 b22 + ⋯ + amn bm1 am1 b12 + am2 b22 + ⋯ + amn bm2 ⋯ a

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.

1) The product of a (4 × 4) matrix


with a (4 × 1) matrix will result in in
a column matrix .
True
False

2) The product of a (3 × 2) matrix and


any matrix with 2 rows can be
calculated; i.e. the number of
columns in the second matrix does
©zyBooks 06/05/19 13:47 471670
not matter. Berkeley Skuratowicz
True OREGONSTATEENGR112AlAbdrabbuhSpring2019

False

3) The product of (1 × 4) matrix with


a (4 × 1) matrix will result in in a
matrix with more than one entry.

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.

©zyBooks 06/05/19 13:47 471670


The length of a vector equals the number of entries of the vector.
Berkeley Skuratowicz
OREGONSTATEENGR112AlAbdrabbuhSpring2019
1) When an (m × n) matrix A and an
(n × 1) column vector b are

multiplied, what is the length of Ab


is equal to?

Answer eld

Check Show answer

2) When a (1 × m) row vector c and


an (m × n) matrix A are
multiplied, the length of cA is equal
to?

Answer eld

Check Show answer

The product of a (m × n) matrix A with a (n × 1) column vector b is:

a11 a12 ⋯ a1n b1 a11 b1 + a12 b2 + ⋯ + a1n bn


⎛ ⎞ ⎛ ⎞ ⎛ ⎞

⎜ a21 a22 ⋯ a2n ⎟ ⎜ b2 ⎟ ⎜ a21 b1 + a22 b2 + ⋯ + a2n bn ⎟


⎜ ⎟ ⎜ ⎟ ⎜ ⎟
Ab =   =
⎜ ⎟ ⎜ ⎟ ⎜ ⎟
⎜ ⋮ ⋮ ⋮ ⎟ ⎜ ⋮ ⎟ ⎜ ⋮ ⎟

⎝ ⎠ ⎝ ⎠ ⎝ ⎠
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

Example 9.3.2: Linear equations: Row-vector perspective.

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

An alternative perspective of how Ab is calculated is:

a11 b1 + a12 b2 + ⋯ + a1n bn a11 a12 a1


⎛ ⎞ ⎛ ⎞ ⎛ ⎞ ⎛

⎜ a21 b1 + a22 b2 + ⋯ + a2n bn ⎟ ⎜ a21 ⎟ ⎜ a22 ⎟ ⎜ a2


⎜ ⎟ ⎜ ⎟ ⎜ ⎟ ⎜
Ab = = b1 + b2 + ⋯ + bn
⎜ ⎟ ⎜ ⎟ ⎜ ⎟ ⎜
⎜ ⋮ ⎟ ⎜ ⋮ ⎟ ⎜ ⋮ ⎟ ⎜ ⋮
⎝ ⎠ ⎝ ⎠ ⎝ ⎠ ⎝
am1 b1 + am2 b2 + ⋯ + amn bn am1 am2 am

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.

©zyBooks 06/05/19 13:47 471670


Example 9.3.3: Linear equations: Column-vector perspective.
Berkeley Skuratowicz
OREGONSTATEENGR112AlAbdrabbuhSpring2019

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:

a11 a12 a1n


⎛ ⎞ ⎛ ⎞ ⎛ ⎞

⎜ a21 ⎟ ⎜ a22 ⎟ ⎜ a2n ⎟


⎜ ⎟ ⎜ ⎟ ⎜ ⎟
Ax = x1 + x2 + ⋯ + xn = x1 a1 + x2 a2 + ⋯ + xn a
⎜ ⎟ ⎜ ⎟ ⎜ ⎟
⎜ ⋮ ⎟ ⎜ ⋮ ⎟ ⎜ ⋮ ⎟

⎝ ⎠ ⎝ ⎠ ⎝ ⎠
am1 am2 amn

PARTICIPATION
ACTIVITY
9.3.8: Valid matrix-matrix operations.

1) If A has the same number of rows


as the number of columns in B ,
then the product AB is allowed.
True
False

2) The matrix product AB always will


equal the BA
True
False
©zyBooks 06/05/19 13:47 471670
Berkeley Skuratowicz
OREGONSTATEENGR112AlAbdrabbuhSpring2019
The multiplication of moderate size matrices by hand is formidable task, but doing so in
MATLAB® is simple.

Example 9.3.4: Matrix multiplication: 1 D arrays.

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.

Example 9.3.5: Matrix multiplication: 2 D arrays.

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

Example 9.3.6: Matrix-vector calculation of the sum of the rows.

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

Different ways to view the multiplication of matrices

Matrix multiplication AB viewed in different ways:

1. Matrix-column matrix: Each column of AB is the product of A with the corresponding co


AB equals A times column vector j of B :

b11 b12 b1n


⎛ ⎛ ⎞ ⎛ ⎞ ⎛ ⎞ ⎞

⎜ ⎜ b21 ⎟ ⎜ b22 ⎟ ⎜ b2n ⎟ ⎟


⎜ ⎜ ⎟ ⎜ ⎟ ⎜ ⎟ ⎟
AB = A A ⋯ A = ( Ab1
⎜ ⎜ ⎟ ⎜ ⎟ ⎜ ⎟ ⎟
⎜ ⎜ ⋮ ⎟ ⎜ ⋮ ⎟ ⎜ ⋮ ⎟ ⎟

⎝ ⎝ ⎠ ⎝ ⎠ ⎝ ⎠ ⎠
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 :

( a11 a12 ⋯ a1n ) B


⎛ ⎞

⎜ ( a21 a22 ⋯ a2n ) B ⎟


⎜ ⎟
AB =
⎜ ⎟
⎜ ⋮ ⎟
©zyBooks 06/05/19 13:47 471670 ⎠

( am1 am2 ⋯ amn ) B
Berkeley Skuratowicz
OREGONSTATEENGR112AlAbdrabbuhSpring2019
3. Scalar/Inner/Dot product matrix: The matrix entry ij of AB is the scalar product between
j-th column vector of B :

https://learn.zybooks.com/zybook/OREGONSTATEENGR112AlAbdrabbuhSpring2019/chapter/9/print 25/29
6/5/2019 ENGR 112: Introduction to Engineering Computing home

(a a12 ⋯ a1n ) b11 b12


⎛ 11 ⎞ ⎛⎛ ⎞⎛ ⎞

⎜ (a a22 ⋯ a2n ) ⎟ ⎜ ⎜ b21 ⎟ ⎜ b22 ⎟


21
⎜ ⎟ ⎜⎜ ⎟ ⎜ ⎟
AB =  
⎜ ⎟ ⎜⎜ ⎟ ⎜ ⎟
⎜ ⋮ ⎟ ⎜⎜ ⋮ ⎟ ⎜

⎝ ⎠ ⎝⎝ ⎠⎝ ⎠
(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
⎛⎛ ⎞⎛ ⎞ ⎛ ⎞ ⎞⎛

⎜ ⎜ a21 ⎟ ⎜ a22 ⎟ ⎜ a2n ⎟ ⎟ ⎜ ( b21 b22


⎜ ⎜ ⎟ ⎜ ⎟ ⎜ ⎟ ⎟ ⎜
AB = ⋯
⎜ ⎜ ⎟ ⎜ ⎟ ⎜ ⎟ ⎟ ⎜
⎜ ⎜ ⎟ ⎜ ⎟ ⎜ ⎟ ⎟ ⎜
⋮ ⋮ ⋮
⎝⎝ ⎠⎝ ⎠ ⎝ ⎠ ⎠⎝
am1 am2 amk ( bm1 bm2

a11 a12
⎛ ⎞ ⎛ ⎞

⎜ a21 ⎟ ⎜ a22 ⎟
⎜ ⎟ ⎜ ⎟
AB = ( ) + ( b2n ) + ⋯
⎜ ⎟ b11 b12 ⋯ b1n ⎜ ⎟ b21 b22 ⋯
⎜ ⎟ ⎜ ⎟
⋮ ⋮
⎝ ⎠ ⎝ ⎠
am1 am2

The different viewpoints of matrix-matrix multiplication are numerically demonstrated with 3 ×

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

gallons/miles used by all vehicles.


Suppose that a vehicle manufacturer sells three types of vehicles and that the number
sold and fuel economy of each vehicle is stored in two 1D row arrays:
numVehicles = [ 110, 32, 89] and vehicleMPG = [ 67.9, 13.1, 34.0].
Given these variables, answer the following. Write arrays as [ 1, 2, 3 ].
©zyBooks 06/05/19 13:47 471670
1) Write an expression to calculate Berkeley Skuratowicz
OREGONSTATEENGR112AlAbdrabbuhSpring2019
[n 1 /c1 , n 2 /c2 , n 3 /c3 ] a row array

containing the gallons/miles in each vehicle


category and assigns the array to wAgMPG. Use
element wise array operators and the given
array variables.

https://learn.zybooks.com/zybook/OREGONSTATEENGR112AlAbdrabbuhSpring2019/chapter/9/print 27/29
6/5/2019 ENGR 112: Introduction to Engineering Computing home

wAgMPG =
Answer eld

Check Show answer

2) Write an expression that calculates the total


number of vehicles sold and assigns it to ©zyBooks 06/05/19 13:47 471670
Berkeley Skuratowicz
totVehicles. Only use a single array OREGONSTATEENGR112AlAbdrabbuhSpring2019
operator, a constant array produced by the
function ones and the given array variables.

totVehicles =
Answer eld

Check Show answer

3) Write an expression to calculate CAFE using


only totVehicles, wAgMPG, and the row array
[1,1,1]

CAFE =
Answer eld

Check Show answer

PARTICIPATION
ACTIVITY
9.3.11: Matrix multiplication.

For the following matrices :

1 5 3
xMat = ( )
3 2 2

©zyBooks 06/05/19 13:47 471670


4 1
⎛ ⎞ Berkeley Skuratowicz
yMat = ⎜2
OREGONSTATEENGR112AlAbdrabbuhSpring2019
3⎟
⎝ ⎠
5 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.

1) What matrix is returned by the


expression xMat * yMat? Write the
matrix in the format of [1, 2, 3; 4, 5,
6].
©zyBooks 06/05/19 13:47 471670
Answer eld Berkeley Skuratowicz
OREGONSTATEENGR112AlAbdrabbuhSpring2019
Check Show answer

2) What size matrix is returned by the


expression yMat * xMat? Write your
answer as n x m.
Answer eld

Check Show answer

3) Complete the following expression


xMat + ?????
to yield the result
ans = [5, 7, 8; 4, 5, 4].
Use the variable yMat.

xMat + Answer eld

Check Show answer

Exploring further: Matrix multiplication

Video: Multiplication of Matrices by Gilbert Strang (up to the 18 mins)

©zyBooks 06/05/19 13:47 471670


Berkeley Skuratowicz
OREGONSTATEENGR112AlAbdrabbuhSpring2019

https://learn.zybooks.com/zybook/OREGONSTATEENGR112AlAbdrabbuhSpring2019/chapter/9/print 29/29

You might also like