[英]How to turn the following into a loop?
本質上,我正在創建各種 arrays 以引發觸覺刺激(刺激傳遞時 0 和 1 的字符串),用於隨機條件,其中刺激之間的間隔來自均勻分布。 我正在使用以下代碼創建 arrays(一切正常):
def rand_stim_seconds(array):
lst = list(range(0,10))
random_ints = []
for i in lst: # Creating list containing intervals drawn from distribution.
rand_int = np.random.uniform(0.1, 0.6, 1)
random_ints.append(rand_int)
values = [] # Convert list from seconds into values of array.
for j in random_ints:
d = j * 22050 # Mutiply by sampling frequency.
e = float(d)
f = int(np.round(e))
values.append(f)
add_lst = [0] # Initialise with a value.
for k in values: # Adding a '1' at specific intervals of the array.
add = add_lst[-1] + k
if add > 44100:
break
add_lst.append(add)
print(add_lst)
for l in add_lst[1:]:
array[l] = 1
return array
I am now trying to create 10 arrays using this function which will then output 10 different arrays (called "stim1" "stim2" etc) which will each have a different pattern of stimulation where each interval is randomly drawn. 以下代碼是我想轉換為循環的代碼:
rand1 = np.zeros((44100, 1))
rand2 = np.zeros((44100, 1))
rand3 = np.zeros((44100, 1))
rand4 = np.zeros((44100, 1))
rand5 = np.zeros((44100, 1))
rand6 = np.zeros((44100, 1))
rand7 = np.zeros((44100, 1))
rand8 = np.zeros((44100, 1))
rand9 = np.zeros((44100, 1))
rand10 = np.zeros((44100, 1))
stim1 = rand_stim_seconds(rand1)
stim2 = rand_stim_seconds(rand2)
stim3 = rand_stim_seconds(rand3)
stim4 = rand_stim_seconds(rand4)
stim5 = rand_stim_seconds(rand5)
stim6 = rand_stim_seconds(rand6)
stim7 = rand_stim_seconds(rand7)
stim8 = rand_stim_seconds(rand8)
stim9 = rand_stim_seconds(rand9)
stim10 = rand_stim_seconds(rand10)
我需要為我希望創建的每個數組創建新的 arrays 0,否則它會覆蓋。 有誰知道如何將上述內容變成一個整潔的循環,因為我非常清楚它是多么業余(我是 Python 新手)! 謝謝!
如果您需要引用所有rand
對象:
rand_list = [np.zeros((44100, 1)) for _ in range(10)]
stim_list = [rand_stim_seconds(r) for r in rand_list]
如果您只需要stim
參考:
stim_list = [rand_stim_seconds(np.zeros((44100, 1))) for _ in range(10)]
以前的stim1
現在是stim[0]
等等。 我正在使用列表理解,因為它是最短的。
以下代碼應該可以解決問題:
stim = [] # Storing stim values in a list
n = 10
for i in range(n):
rand = np.zeroes((44100,1))
stim.append(rand_stim_seconds(rand))
您可以通過他們的索引作為第一個元素的stim[0]
訪問您的刺激 arrays 上的任何內容
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.