Python如何在numpy中组合两个矩阵

ilo*_*cp3 24 python numpy matrix

Python的新手,在numpy中挣扎,希望有人可以帮助我,谢谢!

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
Run Code Online (Sandbox Code Playgroud)

结果:

A= [[ 1.  2.]
   [ 3.  4.]]
B= [[ 5.  6.]]
C= [[ 1.  2.]
   [ 3.  4.]
   [ 5.  6.]]
Run Code Online (Sandbox Code Playgroud)

问题:如何使用A和B生成C,就像在matlab中一样C=[A;B]

Ash*_*ary 35

用途numpy.concatenate:

>>> import numpy as np
>>> np.concatenate((A, B))
matrix([[ 1.,  2.],
        [ 3.,  4.],
        [ 5.,  6.]])
Run Code Online (Sandbox Code Playgroud)


Rom*_*kar 16

你可以使用numpy.vstack:

>>> np.vstack((A,B))
matrix([[ 1.,  2.],
        [ 3.,  4.],
        [ 5.,  6.]])
Run Code Online (Sandbox Code Playgroud)

  • 还有`np.hstack` (3认同)