Matrix-Vector Dot Product

Matrix-Vector Dot Product

  1. Question 1 From deep-ml.com Essentially a dot product of a matrix and a vector an happen only if the number of columns in the matrix is equal to the number of elements in the vector.

That is if A[m,n] and B[n], then A*B is defined.

So in this case, the matrix is 2x3 and the vector is 3x1, so the dot product is defined.

The result is a 2x1 matrix.

The result is:

$$ \begin{bmatrix} 1 & 2 & 3 \\ 4 & 5 & 6 \\ \end{bmatrix} \cdot \begin{bmatrix} 1 \\ 2 \\ 3 \\ \end{bmatrix} \equiv \begin{bmatrix} 1 \cdot 1 + 2 \cdot 2 + 3 \cdot 3 \\ 4 \cdot 1 + 5 \cdot 2 + 6 \cdot 3 \\ \end{bmatrix} \equiv \begin{bmatrix} 14 \\ 32 \\ \end{bmatrix} $$

Solution

 1def matrix_dot_vector(a: list[list[int|float]], b: list[int|float])
 2 -> list[int|float]:
 3	# Return a list where each element is the dot product of a row of 'a' with 'b'.
 4	# If the number of columns in 'a' does not match the length of 'b', return -1.
 5    if len(a[0]) != len(b):
 6        return -1
 7    c = []
 8    for i in range(len(a)):
 9        sum = 0
10        for j in range(len(a)):
11            sum += a[i][j]*b[j]
12
13        c.append(sum)
14	return c

Reply to this post by email ↪