Create Null Matrix in Python using Multiple Methods

Hey fellow Python coder! In this tutorial, we will explore various ways to create a NULL Matrix in Python programming language. If you are not aware of what a null matrix is then no worries I will guide you through the process.

Introduction to NULL Matrix

In simple words, A NULL matrix, also known as a zero matrix, is a special type of matrix where all of its elements are zero. Let’s also understand some basic characteristics of NULL Matrix before moving ahead with code implementation:

  1. All Elements are Zero: As I mentioned just now, all elements of the Matrix are equal to ZERO.
  2. Additive Identity: A Null Matrix when added with any other Matrix results in the Matrix itself. For instance, A + [0] = A where [0] represents a NULL Matrix.
  3. Scalar Multiplication: A Null Matrix when multiplied by any scalar value results in ZERO. For instance [0] * (scalar value) = 0 where [0] again represents a NULL Matrix.
  4. Size of the Matrix: The size of the Matrix is determined by the number of rows and columns present in the matrix. There is no specific rule on whether it has to be a square matrix or any other matrix.

Method 1: Creating NULL Matrix Without the Use of Libraries

1.1 Use the concepts of Nested Loops

We will start with the most basic method to create using the concept of nested loops which manually create each row and column value as done in the code snippet below:

rows = 4
columns = 4
nullMatrix = []

for i in range(rows):
  row = []
  for j in range(columns):
    row.append(0)
  nullMatrix.append(row)

print(nullMatrix)

In the code above, I have first of all fixed the value for the number of rows and columns to keep things simple and also initialized an empty list for the Matrix. Now I am using two loops where the outer loop is responsible for the creation of each row of the matrix. The inner loop is responsible for the creation of each column value in a specific row. As for the NULL Matrix, all values are 0, we are appending the value for each element as 0 only.

Also Read: Nested for Loop in Python

The output of the code looks like this:

[[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]

1.2 Using List Comprehension

The next yet another simple method to create the null matrix is using the concept of List Comprehension. It is a ONE-Liner way to create the matrix in a very concise way using the code snippet below:

rows = 4
columns = 4

nullMatrix = [[0] * columns for i in range(rows)]
print(nullMatrix)

Let’s see what’s happening in the code above. So, the [[0] * columns] code line creates a single row filled with zeros and then we repeat the same list of zeros for the amount of rows mentioned. Pretty simple right?

Also Read: List and dictionary comprehension in python

The output of the code looks like this:

[[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]

Method 2: Creating NULL Matrix with the help of Python Libraries

2.1 Using NumPy Library

When we move on to the mathematical side of Python, the first library that pops into mind is the ‘NumPy’ library which again has multiple functions that can help you create a null matrix for you. The methods are namely zerosand fulland both the functions have their unique styles to create the desired matrix.

Let’s first have a look at the zerosmethod in the code snippet below:

import numpy as np

nullMatrix = np.zeros((4, 4),dtype=int)
print(nullMatrix)

The np.zerosfunction takes a mandatory argument which is the size of the matrix in the form of (rows,columns) . For this tutorial, I am creating a 4 by 4 matrix and hence I have set the size of the matrix as (4,4). The second argument is optional, but the only reason I am adding it here is because by default the data type of resulting matrix is FLOAT but I want to get a matrix in INTEGER form. Hence, I have set the dtypevalue as int.

The output of the code will be as follows:

[[0 0 0 0]
 [0 0 0 0]
 [0 0 0 0]
 [0 0 0 0]]

Also Read: How to create a matrix of random numbers in Python – NumPy

There is another method present in numPy that can also help you achieve a NULL Matrix i.e. full function where there are two mandatory parameters. The first one is pretty obvious that is the size of the matrix and the next is the value by which you wish to fill the entire matrix. For this tutorial, the size is obviously (4,4) and the value that I need to fill the matrix with is ZERO.

Have a look at the code and output below:

import numpy as np

nullMatrix = np.full((4, 4), 0)
print(nullMatrix)

The output of the code will be as follows:

[[0 0 0 0]
 [0 0 0 0]
 [0 0 0 0]
 [0 0 0 0]]

2.2 Using Pandas Library

You will be surprised to know that you can also generate a form of NULL Matrix with the help of the ‘Pandas’ Library. Have a look at the code snippet below:

import pandas as pd

nullMatrix = pd.DataFrame(0, index=range(4), columns=range(4))
print(nullMatrix)

Also Read: Importing dataset using Pandas 

So what exactly is happening in the code is that we are creating a DataFramewhere we are sending three arguments to the function. The first argument specifies the initial value for all elements in the DataFrame (in our case that is ZERO obviously). Now we have a fixed amount of rows and columns which in our case we are considering both as 4. To set the amount of rows and columns we will make use of the indexand columnsarguments of the function respectively. Both functions take a rangevalue that sets the range of indices for rows and columns.

The output of the code will be as follows:

   0  1  2  3
0  0  0  0  0
1  0  0  0  0
2  0  0  0  0
3  0  0  0  0

Pretty Interesting Right?

Conclusion

In this tutorial, we understood what NULL Matrix is and how one can create the same using so many methods in Python programming language. I hope you found this tutorial helpful and learned something new today through this tutorial.

Happy Learning!

Leave a Reply

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