324
324

Reputation: 724

How can I properly append a matrix to the bottom of an existing matrix in Python?

I have two matrices, A and B. A and B are each 100 x 100. I am trying to produce a combined matrix AB such that is 200 x 100 with the elements of A in the first 100 x 100 and the elements of B in the second 100 x 100.

I tried doing the following, but it says the shape is (2, 1, 500, 500) when I do this in Python.

def get_bigAB(n, lamb):
    return np.array([[A], [get_B(n, lamb)]])

My entries are floats, not simple integers.

My get_B function performs as expected, and of course, I am using Python 3.

Upvotes: 0

Views: 1405

Answers (3)

user13301376
user13301376

Reputation:

Try using vstack(A, B) where matrix B is appended to the bottom of matrix A. This will give you the dimensions you want.

Upvotes: 0

324
324

Reputation: 724

Using np.vstack ended up working. Thanks for helping!

def get_Alambda(n, lamb):
    B = get_lambdaI(n, lamb)
    AB = np.vstack((A, B))
    return AB

Upvotes: 1

Björn
Björn

Reputation: 1832

Have you tried numpy concatenate

import numpy as np
AB = np.concatenate((A,B),axis=0)
print(AB)
print(AB.shape)

Upvotes: 0

Related Questions