Abijah
Abijah

Reputation: 546

numpy turning matrixes into vectors and combining them

I have two matricies

a=[[1,2],[3,4]]
b=[[5,6],[7,8]]

and I want to make a matrix where the left column is the elements of a and the right column is the elements of b

c=[[1,5],[2,6],[3,7],[4,8]]

my intuition is that to do this the matricies need to be turned into vectors


EDIT:

I am not sure what types the matricies that I am concerned with are outside this toy example - and haven't been able to use the answers given to work with my matricies.

My matricies come from the code:

#axes 3d wants me to generate 2 matrixes to map onto a wire plot 
x_grid  = np.arange(100, 1000, 100)
y_grid  = np.arange(300, 30000, 100)
x_grid2 = np.matlib.repmat(x_grid,len(x_grid),1)
y_grid2 = np.matlib.repmat(y_grid,len(y_grid),1)

where x_grid2 and y_grid2 take the place of a and b in the toy example. My intuition is that they should be np arrays - but after getting an error and looking thorugh the documentation I am not sure

Upvotes: 0

Views: 34

Answers (1)

Chris Mueller
Chris Mueller

Reputation: 6680

Another possible solution:

import numpy as np

a = np.array([[1, 2], [3, 4]])
b = np.array([[5, 6], [7, 8]])

c = np.array([a.flatten(), b.flatten()]).T

print(c)
# [[1 5]
#  [2 6]
#  [3 7]
#  [4 8]]

Upvotes: 3

Related Questions