Python如何在numpy中组合两个矩阵 [英] Python how to combine two matrices in numpy
本文介绍了Python如何在numpy中组合两个矩阵的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
Python 新手,在 numpy 中挣扎,希望有人能帮帮我,谢谢!
from numpy import *A = 矩阵('1.0 2.0; 3.0 4.0')B = 矩阵('5.0 6.0')C = 矩阵('1.0 2.0; 3.0 4.0; 5.0 6.0')打印 "A=",A打印 "B=",B打印 "C=",C
结果:
A= [[ 1. 2.][ 3. 4.]]B= [[ 5. 6.]]C= [[ 1. 2.][ 3. 4. ][ 5. 6.]]
问题:如何使用A和B生成C,就像在matlab中C=[A;B]
?
解决方案
<预><代码>>>>将 numpy 导入为 np>>>np.concatenate((A, B))矩阵([[ 1., 2.],[3., 4.],[5., 6.]])new to Python, struggling in numpy, hope someone can help me, thank you!
from numpy import *
A = matrix('1.0 2.0; 3.0 4.0')
B = matrix('5.0 6.0')
C = matrix('1.0 2.0; 3.0 4.0; 5.0 6.0')
print "A=",A
print "B=",B
print "C=",C
results:
A= [[ 1. 2.]
[ 3. 4.]]
B= [[ 5. 6.]]
C= [[ 1. 2.]
[ 3. 4.]
[ 5. 6.]]
Question: how to use A and B to generate C, like in matlab C=[A;B]
?
解决方案
Use numpy.concatenate
:
>>> import numpy as np
>>> np.concatenate((A, B))
matrix([[ 1., 2.],
[ 3., 4.],
[ 5., 6.]])
这篇关于Python如何在numpy中组合两个矩阵的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文