Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to double the dimension of a matrix while quartering the values?

I want to achieve this:

a = np.array ([[1, 2],
               [2, 1]])

b = np.array ([[0.25, 0.25, 0.5, 0.5],
               [0.25, 0.25, 0.5, 0.5],
               [0.5, 0.5, 0.25, 0.25],
               [0.5, 0.5, 0.25, 0.25])

Mathematically they r not the same matrices. But i think you get the idea what i want to do. I want to double the dimension of a matrix. But therefore i want to keep the information from the initial matrix a by take the quarter for the four corresponding cells.

Does some1 knows how to do this efficiently in numpy?

like image 339
Marcel H. Avatar asked Dec 08 '25 17:12

Marcel H.


1 Answers

You can use two repeat functions over both axis and then a simple division:

In [8]: np.repeat(np.repeat(a, 2, 1), 2, 0)/4
Out[8]: 
array([[0.25, 0.25, 0.5 , 0.5 ],
       [0.25, 0.25, 0.5 , 0.5 ],
       [0.5 , 0.5 , 0.25, 0.25],
       [0.5 , 0.5 , 0.25, 0.25]])
like image 111
Mazdak Avatar answered Dec 10 '25 21:12

Mazdak



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!