Professional Documents
Culture Documents
Practice Question 2 - Vectors and Linear Functions
Practice Question 2 - Vectors and Linear Functions
Practice Question 2 - Vectors and Linear Functions
(some of the exercises are from the book: Vectors, Matrices and Least Squares (VMLS))
3. Suppose the vector p contains the price of 5 items and q contains the quantity bought of the items.
Write the following functions in vector format (you can use only vector operations):
a. The average price of the items. [for example: if p= [ 10 , 20 ,30 , 40 , 50 ] then the
average_price = 30]
b. Sum of the total cost of the items bought. [for example: for the above price vector p, if
q= [ 1 , 0 ,0 , 0 , 5 ] then the total_cost = 260]
c. Difference between the quantity bought of the 1st and the 3rd item. [for example: if
q= [ 1 , 0 ,0 , 0 , 5 ] then the difference = 1]
d. Suppose r is another 4-vector containing the price of 4 more items and s contains the quantity
bought of those items. Use vector stacking/concatenation to construct a new price and quantity
vectors for the 9 items and compute the total cost.
e. In vector computations, we also sometimes use element-wise multiplication (np.multiply(u,v)
or u * v, for shorthand where u and v are same sized vectors). Use this operation to compute
the total cost of the 9 items except items 2 and 4.
5. Consider an m x n matrix A and an n-vector x and y=f (x )= Ax (Ax means we are multiplying
the matrix A with vector x)
a. What is the size of the input (x) and output (y) of the function f(x)? The function can be
specified as f : R? → R? , what would you write in place of the ‘?’ marks?
b. Suppose all elements of the matrix A is 1, write in words what output will you get by passing
the x to the function?
[ ]
100
c. Suppose A = 0 0 1 , write in words what output you will get by passing a 3-vector x to the
010
function?
[]
1
d. Write down a matrix A that will convert the input vector x= 2 to the output vector
3
[]
3
y= 1 .
2
[]
y1
e. Write down the matrix A, that will produce the output vector y= y2 , such that
y3
y 1=x 1 + x 2 + x 3 , y 2 =2 x 1 +3 x 2 , y 3 =0.5 x 3 .