菜鸟教程 -- 学的不仅是技术,更是梦想!

Python 基础教程
(追記) (追記ここまで)

Python 练习实例44 - Python 两个矩阵相加

Python 100例 Python 100例

两个 3 行 3 列的矩阵,实现其对应位置的数据相加,并返回一个新矩阵:

X = [[12,7,3],
 [4 ,5,6],
 [7 ,8,9]]
Y = [[5,8,1],
 [6,7,3],
 [4,5,9]]

程序分析:创建一个新的 3 行 3 列的矩阵,使用 for 迭代并取出 X 和 Y 矩阵中对应位置的值,相加后放到新矩阵的对应位置中。

程序源代码:

源代码:

#!/usr/bin/python# -*- coding: UTF-8 -*-X = [[12,7,3], [4 ,5,6], [7 ,8,9]]Y = [[5,8,1], [6,7,3], [4,5,9]]result = [[0,0,0], [0,0,0], [0,0,0]]# 迭代输出行foriinrange(len(X)): # 迭代输出列forjinrange(len(X[0])): result[i][j] = X[i][j] + Y[i][j]forrinresult: print(r)

执行以上代码,输出结果如下:

[17, 15, 4]
[10, 12, 9]
[11, 13, 18]

Python 100例 Python 100例

AI 思考中...

9 篇笔记 写笔记

  1. #0

    super

    sup***[email protected]

    13

    参考方法:

    #!/usr/bin/python
    # -*- coding: UTF-8 -*-
    x = [[12,7,3],
     [4,5,6],
     [7,8,9]]
    y = [[5,8,1],
     [6,7,3],
     [4,5,9]]
    z= []
    for i in range(3):
     z.append([])
    for i in range(3):
     for j in range(3):
     z[i].append(x[i][j]+y[i][j])
    print z
    

    super

    sup***[email protected]

    9年前 (2017年04月01日)
  2. #0

    lll

    279***[email protected]

    2

    参考方法:

    #!/usr/bin/python
    # -*- coding: UTF-8 -*-
    x=[[12,7,3],
     [4 ,5,6],
     [7 ,8,9]]
    y=[[5,8,1],
     [6,7,3],
     [4,5,9]]
    c=[]
    d=[]
    for index,element in enumerate(x):
     for i in range(len(element)):
     m=x[index][i]+y[index][i]
     c.append(m)
     if len(c)==3:
     d.append(c)
     c=[]
    print d
    

    lll

    279***[email protected]

    9年前 (2017年04月20日)
  3. #0

    朦胧

    253***[email protected]

    1

    参考方法:

    # coding:utf-8
    import numpy as np
    X = [[12,7,3],
     [4 ,5,6],
     [7 ,8,9]]
    Y = [[5,8,1],
     [6,7,3],
     [4,5,9]]
    Z=np.zeros(shape=(len(X),len(X[0])))
    for i in range(0,len(X)):
     for j in range(0,len(X[0])):
     Z[i][j]=X[i][j]+Y[i][j]
    print(Z)
    

    朦胧

    253***[email protected]

    9年前 (2017年06月13日)
  4. #0

    qingfeng

    297***[email protected]

    2

    参考方法:

    #!/usr/bin/python
    # -*- coding: UTF-8 -*-
    X = [[12,7,3],
     [4 ,5,6],
     [7 ,8,9]]
    Y = [[5,8,1],
     [6,7,3],
     [4,5,9]]
    m = [[],
     [],
     []]
    for (i,j,k) in zip(X,Y,range(3)):
     for (a,b)in zip(i,j):
     m[k].append(a+b)
    for r in m:
     print r
    

    qingfeng

    297***[email protected]

    9年前 (2017年06月13日)
  5. #0

    du_solong

    157***[email protected]

    17

    使用 numpy:

    #!/usr/bin/python
    # coding:utf-8
    import numpy as np
    x = np.array( [[12,7,3],
     [4 ,5,6],
     [7 ,8,9]])
    y = np.array( [[5,8,1],
     [6,7,3],
     [4,5,9]])
    z = x+y
    print z
    

    du_solong

    157***[email protected]

    9年前 (2017年06月14日)
  6. #0

    hello world

    1@q***om

    9

    Python3 使用 random 随机生成两个矩阵:

    import random
    data1 = [[random.randint(1,100) for i in range(3)] for j in range(3)]
    data2 = [[random.randint(1,100) for i in range(3)] for j in range(3)]
    print("data1:",data1)
    print("data2:",data2)
    data3 = data1[:]
    for i in range(len(data1)):
     for j in range(len(data1[i])):
     data3[i][j] = data1[i][j] + data2[i][j]
    print("data3:",data3)
    

    hello world

    1@q***om

    9年前 (2017年06月25日)
  7. #0

    ray

    117***[email protected]

    0

    参考方法:

    # -*- coding: utf-8 -*-
    def matrix():
     X = [[12,7,3],
     [4 ,5,6],
     [7 ,8,9]]
     Y = [[5,8,1],
     [6,7,3],
     [4,5,9]]
     a_list=[]
     a_list_1=[]
     a_list_2=[]
     a_list_3=[]
     for i in range(3):
     for j in range(3):
     if i==0:
     temp=a_list_1
     elif i==1:
     temp=a_list_2
     else:
     temp=a_list_3
     temp.append(X[i][j]+Y[i][j])
     a_list.insert(0,a_list_1)
     a_list.insert(1,a_list_2)
     a_list.insert(2,a_list_3)
     print(a_list)
    matrix()
    

    ray

    117***[email protected]

    9年前 (2017年08月11日)
  8. #0

    sun_shine

    101***[email protected]

    1

    参考方法:

    #!/usr/bin/python
    # -*- coding: UTF-8 -*-
    import numpy as np
    a=np.random.randint(0,100,9).reshape(3,3)
    b=np.random.randint(0,100,9).reshape(3,3)
    print(a+b)
    

    sun_shine

    101***[email protected]

    9年前 (2017年08月13日)
  9. #0

    奚适

    xnx***[email protected]

    3

    使用匿名函数:

    #!/usr/bin/python
    # -*- coding: UTF-8 -*-
    X = [[12,7,3],
     [4,5,6],
     [7,8,9]]
    Y = [[5,8,1],
     [6,7,3],
     [4,5,9]]
    Z = []
    for i in range(3):
     zz = map(lambda a,b:a+b, X[i],Y[i])
     Z.append(zz)
    print Z

    奚适

    xnx***[email protected]

    9年前 (2017年12月21日)

点我分享笔记

  • 昵称 (必填)
  • 邮箱 (必填)
  • 引用地址

AltStyle によって変換されたページ (->オリジナル) /