21
import numpy as np
matrix1 = np.array([[1,2,3],[4,5,6]])
vector1 = matrix1[:,0] # This should have shape (2,1) but actually has (2,)
matrix2 = np.array([[2,3],[5,6]])
np.hstack((vector1, matrix2))
ValueError: all the input arrays must have same number of dimensions

The problem is that when I select the first column of matrix1 and put it in vector1, it gets converted to a row vector, so when I try to concatenate with matrix2, I get a dimension error. I could do this.

np.hstack((vector1.reshape(matrix2.shape[0],1), matrix2))

But this looks too ugly for me to do every time I have to concatenate a matrix and a vector. Is there a simpler way to do this?

Peter Mortensen
31.5k22 gold badges110 silver badges134 bronze badges
asked Jul 12, 2013 at 19:46

3 Answers 3

28

The easier way is

vector1 = matrix1[:,0:1]

For the reason, let me refer you to another answer of mine:

When you write something like a[4], that's accessing the fifth element of the array, not giving you a view of some section of the original array. So for instance, if a is an array of numbers, then a[4] will be just a number. If a is a two-dimensional array, i.e. effectively an array of arrays, then a[4] would be a one-dimensional array. Basically, the operation of accessing an array element returns something with a dimensionality of one less than the original array.

answered Jul 12, 2013 at 19:56

1 Comment

similarly, slicing returns something with the same dimensionality (or rank in numpy terms) as the original array
16

Here are three other options:

  1. You can tidy up your solution a bit by allowing the row dimension of the vector to be set implicitly:

    np.hstack((vector1.reshape(-1, 1), matrix2))
    
  2. You can index with np.newaxis (or equivalently, None) to insert a new axis of size 1:

    np.hstack((vector1[:, np.newaxis], matrix2))
    np.hstack((vector1[:, None], matrix2))
    
  3. You can use np.matrix, for which indexing a column with an integer always returns a column vector:

    matrix1 = np.matrix([[1, 2, 3],[4, 5, 6]])
    vector1 = matrix1[:, 0]
    matrix2 = np.matrix([[2, 3], [5, 6]])
    np.hstack((vector1, matrix2))
    
answered Jul 12, 2013 at 19:55

Comments

0

Subsetting

The even simpler way is to subset the matrix.

>>> matrix1
[[1 2 3]
 [4 5 6]]
>>> matrix1[:, [0]] # Subsetting
[[1]
 [4]]
>>> matrix1[:, 0] # Indexing
[1 4]
>>> matrix1[:, 0:1] # Slicing
[[1]
 [4]]

I also mentioned this in a similar question.

It works somewhat similarly to a Pandas dataframe. If you index the dataframe, it gives you a Series. If you subset or slice the dataframe, it gives you a dataframe.

Your approach uses indexing, David Z's approach uses slicing, and my approach uses subsetting.

answered Nov 6, 2021 at 4:32

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.