How to Create an Empty Matrix in Python

How to Create an Empty Matrix in Python | Previously we have how to create a matrix in Python now, we will discuss how to create an empty matrix in python. Matrix is a rectangular table arranged in the form of rows and columns, in the programming world we implement matrix by using arrays by classifying it as 1D and 2D arrays. In this section, there are some examples to create an empty matrix in python.

How to Create an Empty Matrix in Python

In the below-shown example we have used a library called NumPy, there are many inbuilt functions in NumPy which makes coding easy. Here we have used one such function called empty() which creates an empty matrix.

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

Output:

[ ]

The above code has created a matrix with 0 rows and 2 columns. 

In the below program, we define the number of rows and columns so the matrix is filled with random numbers.

import numpy as np
emp = np.empty((2, 3))
print(emp)

Output:-

[[1.40622703e-316 0.00000000e+000 6.94720513e-310]
[6.94720512e-310 6.94720504e-310 6.94720504e-310]]

Python Program to Create an Empty Matrix

This python program also performs the same task but in different ways. In this program, we will create an empty matrix in python without NumPy.

m, n = 3, 4
matrix = [[0] * n for i in range(m)]

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Python Program Examples

Leave a Comment

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