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

How to transform every element of numpy array into an array of size N filled with the element?

I have a numpy array a the shape of which is (m, n).
I want to transform this array into an array b with shape (m, n, l), where:

b[i,j].length == l
b[i,j,k] == a[i,j]

0 <= i < m
0 <= j < n
0 <= k < l

For example:

m = 2
n = 3
a = [[1,2,3],[4,5,6]]

If l = 2, then b:

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

b = [[[1,1],[2,2],[3,3]],[[4,4],[5,5],[6,6]]]

How can I do it? Is there an easy one-line solution?

>Solution :

This does it:

np.repeat(a, l).reshape(a.shape + (-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