l have three arrays to append. Here a sample of my vectors :
V1=array([ 0.03317591, -0.01624349, -0.01151019])
V2=array([[ 0.06865846, -0.00223798],
[-0.
To use np.hstack
, we need to convert V1
to 2D
such that the lengths along the first axis for the three input arrays are the same -
np.hstack((V1[:,None],V2,V3))
As alternatives, we can use np.column_stack or np.concatenate along the second axis on 2D
converted V1
alongwith others or np.c_ -
np.column_stack((V1,V2,V3))
np.concatenate([V1[:,None],V2,V3],axis=1)
np.c_[V1,V2,V3]
There's nothing wrong with V1 except that it's 1D while V2 and V3 are 2D. According to the docs for hstack, all the input arrays have to have the same shape on all except the second axis. V1 in your code doesn't have a second axis.
You can easily add an empty second axis to V1 during your call to hstack like this:
new_v = hstack((V1[:, None], V2, V3))
That should achieve your desired output.
Note: The V1[:, None]
bit is one of three ways that NumPy has available to add empty dimensions to arrays. The other two are V1[:, np.newaxis]
and the function version np.expand_dims(V1, axis=1)
.
You could use any of these in place of V1[:, None]
in that line of code and it would work. Check out the docs for numpy.expand_dims for more on adding dimensions to arrays.