天天看点

python如何对两个矩阵进行拼接_Python如何在numpy中合并两个矩阵

python如何对两个矩阵进行拼接_Python如何在numpy中合并两个矩阵

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]?

解决方案>>> import numpy as np

>>> np.concatenate((A, B))

matrix([[ 1., 2.],

[ 3., 4.],

[ 5., 6.]])