Table of Contents
In the world of programming, matrices are represented as 2-D arrays used for data representation in rows and columns. These are used in various mathematical and statistical operations and they retain their shape. A matrix with an equal number of rows and columns is called a square matrix.
To create matrix in Python, we can use the numpy
arrays or lists. We will discuss how to create matrix in Python in this article.
Using lists to create matrix in Python
A list also stores a collection of elements at particular positions. A list can store elements of different types under a single list and mathematical operations cannot be applied directly to a list.
We can use it to create matrix in Python.
Using the nested lists to create matrix
A nested list is a list within a list and can be used to create matrix in Python.
A very simple example is shown below.
1 2 3 4 5 |
m = [[4,1,2],[7,5,3],[9,6,9]] for i in m: print(i) |
Output:
1 2 3 4 5 |
[ 4 , 1 , 2 ] [ 7 , 5 , 3 ] [ 9 , 6 , 9 ] |
In the above example, we created a simple nested list that represents a matrix.
Note that we did not print the list directly. This is done because that will print the list in a single line. That is why we iterate through every row individually. We can also do this more cleanly with the join()
function.
See the code below.
1 2 3 4 5 |
m = [[4,1,2],[7,5,3],[9,6,9]] for i in m: print(' '.join(str(i))) |
Output:
1 2 3 4 5 |
[ 4 , 1 , 2 ] [ 7 , 5 , 3 ] [ 9 , 6 , 9 ] |
The difference in using this method to print a matrix is that it joins the elements of a row into a single string before printing it.
Further reading:
Using the for
loop to create matrix
We can use the for
loop to create matrix in Python using lists in Python. If we have a single list then we can iterate it the required number of times within the list, and it will change to a nested list acting as a matrix.
For example,
1 2 3 4 5 |
m = [[4,8,9] for i in range(3)] for i in m: print(' '.join(str(i))) |
Output:
1 2 3 4 5 |
[ 4 , 8 , 9 ] [ 4 , 8 , 9 ] [ 4 , 8 , 9 ] |
This method takes a single list and changes it to a matrix of the required shape.
There is another way to use the for
loop to create matrix in Python.
For example,
1 2 3 4 5 6 7 8 9 10 11 |
lst = [] m = [] for i in range(0,3): for j in range(0,3): lst.append(0) m.append(lst) lst = [] for i in m: print(' '.join(str(i))) |
Output:
1 2 3 4 5 |
[ 0 , 0 , 0 ] [ 0 , 0 , 0 ] [ 0 , 0 , 0 ] |
In the above example,
- We first initialized two empty lists. One will act like a row and the other will act as the final matrix.
- We run a nested loop. The inner loop adds an element to the list
lst
. - In the outer loop,
lst
gets appended to the matrix listm
as a row. - We then initialize the
lst
as an empty list and the loop runs again for the next row.
This method is very convenient if we wish to take the input from the user for every element in a list. For this, we have to simply ask for the input in the inner loop before appending the element to the lst
list.
The final list discussed in the above examples can be directly converted to a numpy
array using the numpy.array()
or the numpy.matrix()
function. These methods will be discussed below.
Using numpy
arrays to create matrix in Python
A numpy
array is used to store a collection of elements of the same type. However, we can apply mathematical operations directly to an array and they can store a longer sequence of data efficiently. That is why it is considered a better approach to use numpy
arrays for matrices in Python.
Let us discuss different methods to create matrix in Python using numpy
arrays.
Using the numpy.array()
function to create matrix in Python
We can create a 2-D numpy
array using the numpy.array()
function in Python.
For example,
1 2 3 4 5 6 |
import numpy as np m = np.array([[4,1,2],[7,5,3],[9,6,9]]) print("Matrix: \n", m) print("Dimensions: \n", m.shape) |
Output:
1 2 3 4 5 6 7 8 |
Matrix: [[4 1 2] [7 5 3] [9 6 9]] Dimensions: (3, 3) |
In the above example, we created a simple matrix in Python. We have verified this by displaying the shape of the matrix, which shows that it is a 2-D array with 3 columns and 3 rows.
Using the numpy.matrix()
function to create matrix in Python
The numpy.matrix()
function is used to return a 2-D array. It works similarly to the numpy.array()
function discussed earlier.
For example,
1 2 3 4 5 6 |
import numpy as np m = np.matrix([[4,1,2],[7,5,3],[9,6,9]]) print("Matrix: \n", m) print("Dimensions: \n", m.shape) |
Output:
1 2 3 4 5 6 7 8 |
Matrix: [[4 1 2] [7 5 3] [9 6 9]] Dimensions: (3, 3) |
Using the numpy.reshape()
function to create matrix in Python
The numpy.reshape()
can also be used to create matrix in Python. This function can be used to alter the shape of the array. We can use it to change the shape of a 1-D array to a 2-D array without changing its elements.
See the code below.
1 2 3 4 5 6 |
import numpy as np m = np.array([[4,1,2,7,5,3,9,6,9]]).reshape(3,3) print("Matrix: \n", m) print("Dimensions: \n", m.shape) |
Output:
1 2 3 4 5 6 7 8 |
Matrix: [[4 1 2] [7 5 3] [9 6 9]] Dimensions: (3, 3) |
Let us discuss what happened in the above example.
- We had a 1-D array consisting of nine elements in total.
- The
reshape()
function modified this to a 2-D array with a shape of(3,3)
. - All the elements were arranged in the matrix with 3 rows and 3 columns.
Note that we have to be careful of the number of elements in an array before changing its shape. Since a matrix of shape (3,3)
will have 9 elements, the above example won’t work if the total elements in the 1-D array are not 9.
Using the numpy.append()
function to create matrix in Python
We can use the numpy.append()
function to create matrix in Python out of an existing array. The way we achieve this is by appending rows to an existing array.
See the code below.
1 2 3 4 5 6 7 |
import numpy as np m = np.array([[4,1,2]]) new = np.append(m,[[7,5,3],[9,6,9]], axis = 0) print("Matrix: \n", new) print("Dimensions: \n", new.shape) |
Output:
1 2 3 4 5 6 7 8 |
Matrix: [[4 1 2] [7 5 3] [9 6 9]] Dimensions: (3, 3) |
In the above example,
- We had a 1-D array called
m
containing a total of three elements. - We used the
append()
function to add two more rows to the existing array and create a new array callednew
. - The
axis
parameter is specified as0
in theappend()
function because we wish to add the elements as rows. - The
new
array is a matrix with a shape of(3,3)
.
That’s all about how to create matrix in Python.