概述
矩阵 python 加法
Here you will get program for python matrix addition. In python “list” concept is there rather than arrays. For implementing matrix we should take help of lists in python. Matrix can be represented as nested lists.
在这里,您将获得用于python矩阵加法的程序。 在python中,“列表”概念存在,而不是数组。 为了实现矩阵,我们应该使用python中的列表。 矩阵可以表示为嵌套列表。
Suppose if we want to implement 2×3 matrix then python syntax will look like this.
假设如果要实现2×3矩阵,则python语法将如下所示。
Matrix=[[1,2,3],[4,5,6]] #This is 2×3 matrix
Matrix = [[1,2,3],[4,5,6]]#这是2×3矩阵
If we want to implement 3×2 matrix then
如果我们要实现3×2矩阵
Matrix=[[1,2],[3,4],[5,6]] #This is 3×2 matrix.
Matrix = [[1,2],[3,4],[5,6]]#这是3×2的矩阵。
Python矩阵加法 (Python Matrix Addition)
Below is python program for addition of two matrices. Program is made and tested in python 3.
以下是用于添加两个矩阵的python程序。 该程序是在python 3中制作和测试的。
def print_matrix(matrix):
for i in range(len(matrix)):
for j in range(len(matrix[0])):
print("t",matrix[i][j],end=" ")
print("n")
def main():
m=int(input("enter rows"));
n=int(input("enter columns"));
#in python initilization is needed before indexing.
matrix1=[[0 for j in range(0,n)] for i in range(0,m)] # matrix 1 initialization with 0s
matrix2=[[0 for j in range(0,n)] for i in range(0,m)] #matrix 2 intialization with 0s
res_matrix=[[0 for j in range(0,n)] for i in range(0,m)] # matrix for storing result
print("enter first matrix elements")
for i in range(0,m):
for j in range(0,n):
matrix1[i][j]= int(input("enter an element"))
print("enter second matrix elements ")
for i in range(0,m):
for j in range(0,n):
matrix2[i][j]=int(input("enter an element"))
for i in range(0,m):
for j in range(0,n):
res_matrix[i][j]=matrix1[i][j]+matrix2[i][j]
#print input matrices
print(" matrix 1")
print_matrix(matrix1)
print(" matrix 2")
print_matrix(matrix2)
# printing resultant matrix
print("resultant matrix after adding")
print_matrix(res_matrix)
main()
Output
输出量
翻译自: https://www.thecrazyprogrammer.com/2018/01/python-matrix-addition.html
矩阵 python 加法
最后
以上就是灵巧曲奇为你收集整理的矩阵 python 加法_Python矩阵加法的全部内容,希望文章能够帮你解决矩阵 python 加法_Python矩阵加法所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复