0% found this document useful (0 votes)
11 views21 pages

Matrices

The document provides an overview of how to create and manipulate matrices in Python using both nested lists and the Numpy library. It covers matrix creation, addition, subtraction, multiplication, and transposition, along with examples of how to access specific elements and rows. The document emphasizes the efficiency of Numpy for matrix operations compared to traditional lists.

Uploaded by

Gautham J K
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views21 pages

Matrices

The document provides an overview of how to create and manipulate matrices in Python using both nested lists and the Numpy library. It covers matrix creation, addition, subtraction, multiplication, and transposition, along with examples of how to access specific elements and rows. The document emphasizes the efficiency of Numpy for matrix operations compared to traditional lists.

Uploaded by

Gautham J K
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

Module -II

Matrices in Python
Matrices in Python
• Python does not have a straightforward way to implement a matrix
data type.

• The python matrix makes use of arrays, and the same can be
implemented.

• Create a Python Matrix using the nested list data type


• Create Python Matrix using Arrays from Python Numpy package
Create Python Matrix using a nested list data
type
• The matrix inside a list with all the rows and columns is as shown
below:

• List = [[Row1],
[Row2],
[Row3]
...
[RowN]]
Create Python Matrix using a nested list data
type
Create Python Matrix using a nested list data
type
• M1 = [[8, 14, -6],
[12,7,4],
[-11,3,21]]

#To print the matrix


print(M1)

M1 = [[8, 14, -6], [12, 7, 4], [-11, 3, 21]]


To print the rows in the Matrix
M1 = [[8, 14, -6],
[12,7,4],
[-11,3,21]]

matrix_length = len(M1)

#To print the rows in the Matrix


for i in range(matrix_length):
print(M1[i])
To read the last element from each row
M1 = [[8, 14, -6],
[12,7,4],
[-11,3,21]]

matrix_length = len(M1)

#To read the last element from each row


for i in range(matrix_length):
print(M1[i][-1])
To read from the last element from each row
Adding Matrices Using Nested List
Matrix 1: initialize a matrix that will store the result of M1 + M2
M1 = [[8, 14, -6], Matrix 3 :
[12,7,4], M3 = [[0,0,0],
[-11,3,21]] [0,0,0],
[0,0,0]]
Matrix 2 :

M2 = [[3, 16, -6],


[9,7,-4],
[-1,3,13]]
Adding Matrices Using Nested List
Take Matrix input from user in Python
Create Python Matrix using Arrays from Python
Numpy package
• The python library Numpy helps to deal with arrays and matrices

• Numpy processes an array a little faster in comparison to the list.

To work with Numpy


• Step 1 - Install Numpy : pip install numpy
• Step 2 – Import Numpy in your code (import numpy as np)
Array in Numpy to create Python Matrix
import numpy as np
M1 = np.array([[5, -10, 15], [3, -6, 9], [-4, 8, 12]])
print(M1)

• pass a list, tuple or any array-like object into the array() method, and it will be
converted into an ndarray (short name for N-dimensional array)

• The array object in NumPy is called ndarray


Matrix Addition
To perform addition on the matrix
• create two matrices using numpy.array()
• add them using the (+) operator.

import numpy as np

M1 = np.array([[3, 6, 9], [5, -10, 15], [-7, 14, 21]])


M2 = np.array([[9, -18, 27], [11, 22, 33], [13, -26, 39]])
M3 = M1 + M2
print(M3)

[[ 12 -12 36]
[ 16 12 48]
[ 6 -12 60]]
Matrix Subtraction
To perform subtraction on the matrix, we will create two matrices using
numpy.array() and subtract them using the (-) operator.

import numpy as np
M1 = np.array([[3, 6, 9], [5, -10, 15], [-7, 14, 21]])
M2 = np.array([[9, -18, 27], [11, 22, 33], [13, -26, 39]])
M3 = M1 - M2
print(M3)

[[ -6 24 -18]
[ -6 -32 -18]
[-20 40 -18]]
Matrix Multiplication
• To multiply them will, you can make use of numpy dot() method.
• Numpy.dot() is the dot product of matrix M1 and M2.
• Numpy.dot() handles the 2D arrays and perform matrix multiplications
• import numpy as np

M1 = np.array([[3, 6], [5, -10]])


M2 = np.array([[9, -18], [11, 22]])
M3 = M1.dot(M2)
print(M3)
[[ 93 78]
[ -65 -310]]
Matrix Transpose
• The transpose of a matrix is calculated, by changing the rows as
columns and columns as rows

import numpy as np
M1 = np.array([[3, 6, 9], [5, -10, 15], [4,8,12]])
M2 = M1.transpose()
print(M2)
[[ 3 5 4]
[ 6 -10 8]
[ 9 15 12]]
To print all rows and third columns
import numpy as np
M1 = np.array([[2, 4, 6, 8, 10],
[3, 6, 9, -12, -15],
[4, 8, 12, 16, -20],
[5, -10, 15, -20, 25]])

print(M1[:,3]) # This will print all rows and the third column data.

[ 8 -12 16 -20]
[[ 2 4 6 8 10]]

To print the first row and all columns


import numpy as np
M1 = np.array([[2, 4, 6, 8, 10],
[3, 6, 9, -12, -15],
[4, 8, 12, 16, -20],
[5, -10, 15, -20, 25]])
print(M1[:1,]) # This will print first row and all columns

[[ 2 4 6 8 10]]
To print the first three rows and first 2 columns
import numpy as np
M1 = np.array([[2, 4, 6, 8, 10],
[3, 6, 9, -12, -15],
[4, 8, 12, 16, -20],
[5, -10, 15, -20, 25]])
print(M1[:3,:2])

[[2 4]
[3 6]
[4 8]]
To print the rows of the matrix
import numpy as np
M1 = np.array([[3, 6, 9], [5, -10, 15], [4,8,12]])
print(M1[0]) #first row
print(M1[1]) # the second row
print(M1[-1]) # -1 will print the last row

[3 6 9]
[ 5 -10 15]
[ 4 8 12]

You might also like