How to Multiply 2D Matrices in Numpy?

Daily Data Science Puzzle

import numpy as np

# graphics data
a = [[1, 1],
     [1, 0]]

# stretch vectors
b = [[2, 0],
     [0, 2]]
c = np.matmul(a, b)

print(c[0,1])

What is the output of this puzzle?

Numpy is a popular Python library for data science focusing on arrays, vectors, and matrices.

This puzzle shows an important application domain of matrix multiplication: Computer Graphics.

We create two matrices a and b. The first matrix a is the data matrix (e.g. consisting of two column vectors (1,1) and (1,0)). The second matrix b is the transformation matrix that transforms the input data. In our setting, the transformation matrix simply stretches the column vectors.

More precisely, the two column vectors (1,1) and (1,0) are stretched by factor 2 to (2,2) and (2,0). The resulting matrix is therefore [[2,2],[2,0]]. We access the first row and second column.


Are you a master coder?
Test your skills now!

Related Video

Solution

2

 

Leave a Comment

Your email address will not be published. Required fields are marked *