Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 476
ilovecp3 Avatar asked Nov 24 '13 19:11

ilovecp3


People also ask

How do I merge two matrices in NumPy?

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

How do you merge two matrices in Python?

You can use the numpy. concatenate() function to concat, merge, or join a sequence of two or multiple arrays into a single NumPy array. Concatenation refers to putting the contents of two or more arrays in a single array.

How do you combine two matrices?

You can also use square brackets to join existing matrices together. This way of creating a matrix is called concatenation. For example, concatenate two row vectors to make an even longer row vector. To arrange A and B as two rows of a matrix, use the semicolon.

Can you combine NumPy arrays?

We can perform the concatenation operation using the concatenate() function. With this function, arrays are concatenated either row-wise or column-wise, given that they have equal rows or columns respectively. Column-wise concatenation can be done by equating axis to 1 as an argument in the function.


4 Answers

Use numpy.concatenate:

>>> import numpy as np
>>> np.concatenate((A, B))
matrix([[ 1.,  2.],
        [ 3.,  4.],
        [ 5.,  6.]])
like image 124
Ashwini Chaudhary Avatar answered Oct 02 '22 16:10

Ashwini Chaudhary


You can use numpy.vstack:

>>> np.vstack((A,B))
matrix([[ 1.,  2.],
        [ 3.,  4.],
        [ 5.,  6.]])
like image 35
Roman Pekar Avatar answered Oct 03 '22 16:10

Roman Pekar


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

>>> 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.]])
like image 37
yourstruly Avatar answered Oct 05 '22 16:10

yourstruly


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

like image 26
Py Foliage Avatar answered Oct 03 '22 16:10

Py Foliage