Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Column bind in Numpy 2 array and 1 matrix

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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 ?

>Solution :

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.]])
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading