How to Create a 2D Matrix in Python

How to Create a 2D Matrix in Python | Previously we have seen programs to create a matrix in Python now, we will discuss how to create a 2D 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 a 2D matrix in python.

How to Create a 2D Matrix in Python

In this program, we will create a 2-dimensional matrix in python by using a nested list. The join() method is a string method and returns a string in which the elements of the sequence have been joined by the str separator.

m = [[3, 0, 2], [9, 5, 1], [1, 7, 4]]
for i in m:
   print("".join(str(i)))

Output:

[3, 0, 2]
[9, 5, 1]
[1, 7, 4]

The above output shows a 2-dimensional matrix.

How to Create a 2D Matrix in Python using NumPy

Make a 2D Matrix in Python Using numpy.array()

Now let us see a simple program to make a matrix in Python using NumPy. As mentioned above NumPy is a library that has many functions. In this code, we use numpy.array() to make a matrix.

import numpy as np
m = np.array([[3, 0, 2], [1, 2, 3], [0, 8, 3]])
print(m)

Output:

[[3 0 2]
[1 2 3]
[0 8 3]]

In the shown code, we have imported NumPy as np, then in variable num, we stored an array by using numpy.array() however we can do this in a mathematical way also but it would be a long sequence. That is why using NumPy is a better way of coding.

Python Program to Create a 2-D Matrix using numpy.matrix()

The numpy.matrix() is also a function in NumPy that is used to return an array, it works similar to numpy.array().

import numpy as np
m = np.matrix(([25, 12, 10], [15, 30, 14], [51, 39, 78]))
print(m)

Output:-

[[25 12 10]
[15 30 14]
[51 39 78]]

Make a 2D Matrix in Python using numpy.append()

The numpy.append() is a function that appends an array into the row.

import numpy as np
m = np.array([[2, 4, 9]])
new = np.append(m, [[1, 5, 9], [7, 6, 4]], axis=0)
print(new)

Output:-

[[2 4 9]
[1 5 9]
[7 6 4]]

In the above code, the array num is appended to new so the array gets altered. The axis parameter is set 0 as we wish to append the elements as rows. We have also used shape as it displays the dimension of the matrix. In the above example, we have created a matrix with 3 rows and 3 columns hence dimension is (3,3).

Create a 2-Dimensional Matrix in Python using numpy.reshape()

The numpy.reshape() is a function in NumPy that converts a 1D array to 2D. 

import numpy as np
m = np.array([[9, 8, 7, 6, 5, 4, 3, 2, 1]]).reshape(3, 3)
print(m)

Output:

[[9 8 7]
[6 5 4]
[3 2 1]]

reshape() takes 2 parameters that are row and column, it is basically used to alter the size of the array. In the above example reshape() takes 2 parameters 3 and 3 so it converts 1D array to 2D array 3X3 elements.

How to Create a 2D Matrix in Python without NumPy

The matrix can be created without using NumPy, the below code creates a 2D matrix using the nested list. A nested list is a list within a list.

m = [[4, 9, 3], [2, 0, 6], [0, 5, 2]]
for i in m:
   print(i)

Output:-

[4, 9, 3]
[2, 0, 6]
[0, 5, 2]

In the example shown we have declared variable m as a nested list and then used a for loop to print all the elements, without a for loop the elements have been printed in a single line.

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!

Leave a Comment

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