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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20180210/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 19:46:27  来源:igfitidea点击:

Python how to combine two matrices in numpy

pythonnumpymatrix

提问by ilovecp3

new to Python, struggling in numpy, hope someone can help me, thank you!

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

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

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

采纳答案by Ashwini Chaudhary

Use numpy.concatenate:

使用numpy.concatenate

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

回答by Roman Pekar

You can use numpy.vstack:

您可以使用numpy.vstack

>>> np.vstack((A,B))
matrix([[ 1.,  2.],
        [ 3.,  4.],
        [ 5.,  6.]])

回答by yourstruly

If You want to work on existing array C, you could do it inplace:

如果你想在现有的数组 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.]])

回答by Py Foliage

For advanced combining (you can give it loop if you want to combine lots of matrices):

对于高级组合(如果你想组合大量矩阵,你可以给它循环):

# 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)

The result:

结果:

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

Credit: I edit yourstruly answer and implement what I already have on my code

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