Transpose of a Matrix in Python with user input
Transpose of a Matrix in Python with user input
For Example:
print('matrix: ')
for i in matrix:
print(i)
for i in range(row):
for j in range(col):
transpose[j][i] = matrix[i][j]
for i in transpose:
print(i)
Output
matrix:
[1, 2, 3]
[4, 5, 6]
[7, 8, 9]
Transpose of matrix:
[1, 4, 7]
[2, 5, 8]
[3, 6, 9]
Subtraction of Two Matrix in python with User input
You can subtract two matrices if the number of rows and number of columns is
the same for both the matrix.
Example:
Ask the user for the input of rows (m) & columns (n) of the matrix.
Using input()function and list comprehension create matrix_A and
matrix_B of dimension m x n.
For storing the result, create another matrix (result) of the same
dimension m x n and initially, it is Zero.
Using nested for-loop do result[i][j] = matrix_A[i][j] - matrix_B[i][j] .
At last print the resultant matrix(result).
Source Code
print('Matrix-A :')
for i in matrix_A:
print(i)
print('Matrix-B :')
for i in matrix_B:
print(i)
for i in range(rows):
for j in range(cols):
result[i][j] = matrix_A[i][j] - matrix_B[i][j]
for i in result:
print(i)
Output:
[6, 2]
[7, 9]
Matrix-B :
[1, 2]
[3, 4]
[5, 0]
[4, 5]
Source Code
def rotate_matrix(matrix):
transposed = []
for row in zip(*matrix):
transposed.append(list(row))
rotated = []
rotated.append(row[::-1])
return rotated
print()
print(row)
rotate_90 = rotate_matrix(matrix)
print()
print(row)
Output
Given matrix :
[1, 2, 3]
[4, 5, 6]
[7, 8, 9]
[7, 4, 1]
[8, 5, 2]
[9, 6, 3]
Find Inverse of a Matrix Using Numpy
In this post, we will learn how to find inverse of a matrix using numpy with
detailed exaplanation and example.
The matrix should be a square matrix (the number of rows and columns must be
the same)
The determinant of the matrix should be non-zero (det(A) ≠ 0), or you can say
the matrix is non-singular.
We are all set to jump into our programming part because we now have a basic
understanding of how to calculate the inverse of a matrix.
All the above information is for your understanding. In the numpy library, there
is one module called linalg (numpy.linalg). Inside that module, there is
one built-in function called inv (numpy.linalg.inv()) that helps us to
invert a matrix very easily in just one line. Let’s see a little more about this
function.
numpy.linalg.inv()
Source Code
import numpy as np
print("Matrix:")
print(matrix)
print("\nInverse of a Matrix:")
print(inv_matrix)
Output
[[1 2]
[3 4]]
Inverse of a Matrix:
[[-2. 1. ]
[ 1.5 -0.5]]
Now we know electricity charges and their rates, So now let’s start writing our
program.
Source Code
else:
payment = 0
fixedCharge = 1500
Output