0

I have to concatenate columns of numpy matrix to create new matrix. I have 2 codes. One is working fine. Other is giving me trouble. The one working fine is

x = np.array(range(24))
x = x.reshape((3,4,2))
y = np.array(range(100,124))
y = y.reshape((3,4,2))
z = np.concatenate((x,y))

Now result is 6,4,2 if axis =0 it si 3,8,2 if axis=1 and 3,4,4 if axis=2. But look at another code:

a=np.array(([1,2,3],[4,5,6],[7,8,9])
b=a[:,1] # took one column
c=a[:,0] # again took 1 column
d=np.concatenate((b,c))

If I provide axis=0 result is 1x6. If I provide axis=1 it is again 1,6. I want in 1 case 1,6 in another 3,2. ie

[2,1],[5,4],[8,7]

and in another 2,3 ie

[2,5,8],[1,4,7]

I am wondering why concatenate is not working for me?

1
  • Check the dimensions of each array that you concatenate. To use axis 1, they have to have 2 dimensions. Reshape if needed. Commented Jul 14, 2015 at 8:10

1 Answer 1

1

Use vstack

>>> import numpy as np
>>> a=np.array(([1,2,3],[4,5,6],[7,8,9]))
>>> b=a[:,1] # took one column
>>> c=a[:,0] # again took 1 column
>>> np.vstack((b,c))
array([[2, 5, 8],
       [1, 4, 7]])

Use np.transpose if necessary

Sign up to request clarification or add additional context in comments.

1 Comment

Hey thanks that works. For 3,2 I took transpose of this any direct way of getting 3,2?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.