Consider the following array h_0 = (1/16)*np.array([1,4,6,4,1])
. What is the easiest way to insert N
zeros between each values of h_0
(as part of a function)? So that I get for N=2
for example
>>> array([0.0625, 0. , 0. , 0.25 , 0. , 0. , 0.375 , 0. ,
0. , 0.25 , 0. , 0. , 0.0625])
The simplest is probably slicing:
h_0 = (1/16)*np.array([1,4,6,4,1])
N = 2
out = np.zeros(h_0.size * (N+1) - N , h_0.dtype)
out[::N+1] = h_0
out
# array([0.0625, 0. , 0. , 0.25 , 0. , 0. , 0.375 , 0. ,
# 0. , 0.25 , 0. , 0. , 0.0625])
Reshape h_0
to 2D, stack it with zeros and then flatten the result:
import numpy as np
h_0 = (1/16)*np.array([1,4,6,4,1])
N = 2
zeros = np.zeros((h_0.shape[0], N))
print(np.hstack((h_0[:,None], zeros)).reshape(-1)[:-N])
# [0.0625 0. 0. 0.25 0. 0. 0.375 0. 0. 0.25 0. 0. 0.0625]
You can play with this here .
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.