ilovecp3 ilovecp3 - 7 months ago 29
Python Question

Python how to combine two matrices in 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]? Thank you so much

Answer

Use numpy.concatenate:

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