Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/345.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python如何在numpy中组合两个矩阵_Python_Numpy_Matrix - Fatal编程技术网

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

Python如何在numpy中组合两个矩阵,python,numpy,matrix,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 结果: A= [[ 1. 2.] [ 3. 4.]] B= [[ 5. 6.]] C= [[ 1. 2

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
结果:

A= [[ 1.  2.]
   [ 3.  4.]]
B= [[ 5.  6.]]
C= [[ 1.  2.]
   [ 3.  4.]
   [ 5.  6.]]
问题:如何使用A和B生成C,如在matlab中
C=[A;B]

使用:

您可以使用:


如果您想在现有的array C上工作,您可以在适当的地方进行:

>>> from numpy  import *
>>> A = matrix('1.0 2.0; 3.0 4.0')
>>> B = matrix('5.0 6.0')

>>> shA=A.shape
>>> shA
(2L, 2L)
>>> shB=B.shape
>>> shB
(1L, 2L)

>>> C = zeros((shA[0]+shB[0],shA[1]))
>>> C
array([[ 0.,  0.],
       [ 0.,  0.],
       [ 0.,  0.]])

>>> C[:shA[0]]
array([[ 0.,  0.],
       [ 0.,  0.]])
>>> C[:shA[0]]=A
>>> C[shA[0]:shB[0]]=B
>>> C
array([[ 1.,  2.],
       [ 3.,  4.],
       [ 0.,  0.]])
>>> C[shA[0]:shB[0]+shA[0]]
array([[ 0.,  0.]])
>>> C[shA[0]:shB[0]+shA[0]]=B
>>> C
array([[ 1.,  2.],
       [ 3.,  4.],
       [ 5.,  6.]])
对于高级组合(如果要组合大量矩阵,可以给它循环):

结果是:

# Result
# Original Matrices
# [[1 2 3]
#  [4 5 6]]
# [[7 8]]
# Vertical Combine
# [[1 2 3]
#  [4 5 6]
#  [7 8 0]]
# Horizontal Combine
# [[1 2 3 7 8]
#  [4 5 6 0 0]]
# Diagonal Combine
# [[1 2 3 0 0]
#  [4 5 6 0 0]
#  [0 0 0 7 8]]

信用:我编辑你的答案,并实现我代码中已有的内容

还有
np.hstack
>>> from numpy  import *
>>> A = matrix('1.0 2.0; 3.0 4.0')
>>> B = matrix('5.0 6.0')

>>> shA=A.shape
>>> shA
(2L, 2L)
>>> shB=B.shape
>>> shB
(1L, 2L)

>>> C = zeros((shA[0]+shB[0],shA[1]))
>>> C
array([[ 0.,  0.],
       [ 0.,  0.],
       [ 0.,  0.]])

>>> C[:shA[0]]
array([[ 0.,  0.],
       [ 0.,  0.]])
>>> C[:shA[0]]=A
>>> C[shA[0]:shB[0]]=B
>>> C
array([[ 1.,  2.],
       [ 3.,  4.],
       [ 0.,  0.]])
>>> C[shA[0]:shB[0]+shA[0]]
array([[ 0.,  0.]])
>>> C[shA[0]:shB[0]+shA[0]]=B
>>> C
array([[ 1.,  2.],
       [ 3.,  4.],
       [ 5.,  6.]])
# Advanced combining
import numpy as np

# Data
A = np.matrix('1 2 3; 4 5 6')
B = np.matrix('7 8')
print('Original Matrices')
print(A)
print(B)

# Getting the size
shA=np.shape(A)
shB=np.shape(B)
rowTot=shA[0]+shB[0]
colTot=shA[1]+shB[1]
rowMax=np.max((shA[0],shB[0]))
colMax=np.max((shA[1],shB[1]))

# Allocate zeros to C
CVert=np.zeros((rowTot,colMax)).astype('int')
CHorz=np.zeros((rowMax,colTot)).astype('int')
CDiag=np.zeros((rowTot,colTot)).astype('int')

# Replace C
CVert[0:shA[0],0:shA[1]]=A
CVert[shA[0]:rowTot,0:shB[1]]=B
print('Vertical Combine')
print(CVert)

CHorz[0:shA[0],0:shA[1]]=A
CHorz[0:shB[0],shA[1]:colTot]=B
print('Horizontal Combine')
print(CHorz)

CDiag[0:shA[0],0:shA[1]]=A
CDiag[shA[0]:rowTot,shA[1]:colTot]=B
print('Diagonal Combine')
print(CDiag)
# Result
# Original Matrices
# [[1 2 3]
#  [4 5 6]]
# [[7 8]]
# Vertical Combine
# [[1 2 3]
#  [4 5 6]
#  [7 8 0]]
# Horizontal Combine
# [[1 2 3 7 8]
#  [4 5 6 0 0]]
# Diagonal Combine
# [[1 2 3 0 0]
#  [4 5 6 0 0]
#  [0 0 0 7 8]]