I have two arrays:
dvec = np.repeat(0, k);dvec
mu = np.array([1,2,3,4,5])
Amat = np.stack((dvec, mu), axis=1);
and a matrix
I = np.identity(k)
I want to column bind them in order to have one matrix
Amat = np.stack((np.repeat(1,k), mu,I), axis=1);Amat
but when I do this python reports me
ValueError: all input arrays must have the same shape
How can I solve this error ?
CodePudding user response:
You can use numpy.column_stack: https://numpy.org/doc/stable/reference/generated/numpy.column_stack.html
k=5
dvec = np.repeat(0, k);
mu = np.array([1,2,3,4,5])
I = np.identity(k)
Amat=np.column_stack((dvec,mu,I))
Result:
array([[0., 1., 1., 0., 0., 0., 0.],
[0., 2., 0., 1., 0., 0., 0.],
[0., 3., 0., 0., 1., 0., 0.],
[0., 4., 0., 0., 0., 1., 0.],
[0., 5., 0., 0., 0., 0., 1.]])
CodePudding user response:
This will answer your question:
np.hstack((np.repeat(1, 5).reshape(-1,1), np.array([1,2,3,4,5]).reshape(-1,1), I))
np.stack
joins a sequence of arrays along a new axis.
So it requires them to have the same shape.
What you need is hstack
which stacks arrays in sequence horizontally (column bind)