How to make a matrix in Python

In this Python tutorial, we will discuss how to make a matrix in python. Also, We will see these below topics as:

  • What is the matrix in python?
  • How to create a matrix in python using user input
  • Create an empty matrix using NumPy in python
  • How to create a matrix in python 3
  • How to do matrix multiplication in python
  • How to create a matrix using for loop in python
  • How to create a matrix in Python using a list

What is the matrix in python?

  • A Python matrix is a two-dimensional rectangular array of data stored in rows and columns.
  • The horizontal entries in a matrix are called ‘rows’ and the vertical entries are called ‘columns’.
  • If a matrix has r number of rows and c number of columns then the order of the matrix is given by r x c.
  • The data stored in the matrix can be strings, numbers, etc.
  • In a matrix, data are arranged into rows and columns.
  • Matrix is an important data structure for mathematical and scientific calculation.
  • Python doesn’t have a built-in type for matrices, so we can treat a list of list as a matrix.

You may like Python copy file (Examples) and Crosstab in Python Pandas.

How to create a matrix in python using user input

Let’s see how to create matrix in python:

  • In Python, there exists a popular library called NumPy.
  • To work with NumPy, we need to install it. The command to install NumPy is ‘pip install NumPy’.
  • To make use of NumPy in code, we have to import it as ‘import NumPy as np’.
  • Matrix is created using NumPy.matrix() function.
  • We will take user input for matrix and then it will display a matrix in the output.

Example:

import numpy as np
a = int(input("Enter the number of rows:"))
b = int(input("Enter the number of columns:"))
print("Enter the number in a single line separated by space:")
val = list(map(int, input().split()))
matrix = np.array(val).reshape(a,b)
print(matrix)

After writing the above code (how to create a matrix in python using user input), Once you will print “matrix” then the output will appear as a “[[2 4] [6 3]] ”. Here, np.array().reshape() is used for printing the matrix.

You can refer to the below screenshot how to create a matrix in python using user input.

How to create a matrix in python using user input
How to create a matrix in python using user input

Create an empty matrix using NumPy in python

Here, we will see how to create an empty matrix using NumPy in python.

To create an empty matrix, we will first import NumPy as np and then we will use np.empty() for creating an empty matrix.

Example:

import numpy as np
m = np.empty((0,0))
print(m)

After writing the above code (Create an empty matrix using NumPy in python), Once you will print “m” then the output will appear as a “ [ ] ”. Here, np.empty() matrix of 0 rows and 0 columns is used for creating an empty matrix in python.

You can refer to the below screenshot create an empty matrix using NumPy in python.

Create an empty matrix using NumPy in python
Create an empty matrix using NumPy in python

How to create a matrix in python 3

We will import numpy as np first, and then a matrix is created using numpy.matrix(). In this way, a matrix can be created in python.

Example:

import numpy as np
m = np.matrix([[3, 4], [5, 2]])
print('Matrix is:\n', m)

After writing the above code (how to create a matrix in python 3), Once you will print “m” then the output will appear as a “[[3 4] [5 2]] ”. Here, np.matrix() is used for printing the matrix and it will return the matrix.

You can refer to the below screenshot how to create a matrix in python 3.

How to create a matrix in python 3
How to create a matrix in python 3

How to do matrix multiplication in python

Matrix multiplication is an operation that takes two matrices as an input. To get element-wise multiplication, we can use the np.multiply() function.

Example:

import numpy as np
mat1 = np.matrix([[2, 5],[4, 1]])
mat2 = np.matrix([[6, 5],[4, 7]])
matrix_result = np.multiply(mat1, mat2)
print(matrix_result)

After writing the above code (how to do matrix multiplication in python), Once you will print “matrix_result” then the output will appear as a “[[12 25] [16 7]] ”. Here, np.multiply() is used for multiplying two matrices and it will produce a single matrix after multiplying.

You can refer to the below screenshot how to do matrix multiplication in python.

How to do matrix multiplication in python
How to do matrix multiplication in python

How to create a matrix using for loop in python

Let us see how to create a matrix using for loop in python.

For creating a matrix using for loop we need to take user input. The matrix consists of lists that are created and assigned to columns and rows and the for loop is used for rows and columns.

Example:

c_size = int(input("Enter size of column: "))
r_size = int(input("Enter size of row: "))
x = []
y = []
for j in range(0, c_size):
    y.append(0)
for i in range(0, r_size):
    x.append(y)
print(x)

After writing the above code (how to create a matrix using for loop in python), Once you will print “x” then the output will appear as a “[[0, 0, 0], [0, 0, 0], [0, 0, 0]]”. Here, the user will enter the size of the row and column and it will append the column to each row. The matrix will be printed as an output.

You can refer to the below screenshot how to create a matrix using for loop in python.

How to create a matrix using for loop in python
How to create a matrix using for loop in python

How to create a matrix in Python using a list

Let us see how to create a matrix in Python using a list?

We can create a matrix in Python using a nested list. Firstly we will import NumPy and then we can use np.array() using the list which will give the output as a matrix.

Example:

import numpy as np
mat = np.array([[1, 3, 2], [5, 6, 4]])
print(mat)

After writing the above code (how to create a matrix in Python using a list), Once you will print “mat” then the output will appear as a “[[1 3 2] [5 6 4]]”. Here, the np.array() is used for creating the matrix using a list and it will give the matrix as an output.

You can refer to the below screenshot how to create a matrix in Python using a list

How to create a matrix in Python using a list
How to create a matrix in Python using a list

You may like the following Python tutorials:

In this Python tutorial, we have learned about how to make a matrix in python. Also, We covered these below topics:

  • What is the matrix in python?
  • How to create a matrix in python using user input
  • Create an empty matrix using NumPy in python
  • How to create a matrix in python 3
  • How to do matrix multiplication in python
  • How to create a matrix using for loop in python
  • How to create a matrix in Python using a list