简体   繁体   English

如何将以下内容变成循环?

[英]How to turn the following into a loop?

Essentially I am creating various arrays to elicit tactile stimulation (string of 0s with 1s when stimulation is delivered) for a random condition where the interval between the stimulation is drawn from a uniform distribution.本质上,我正在创建各种 arrays 以引发触觉刺激(刺激传递时 0 和 1 的字符串),用于随机条件,其中刺激之间的间隔来自均匀分布。 I am using the following code to create the arrays (which all works fine):我正在使用以下代码创建 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. 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. The following code is what I would like to convert into a loop:以下代码是我想转换为循环的代码:

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)

I need to create new arrays of 0 for each array I wish to create otherwise it overwrites.我需要为我希望创建的每个数组创建新的 arrays 0,否则它会覆盖。 Does anyone know how to turn the above into a neat loop as I am very aware of how amateur it is (I am new to Python)!有谁知道如何将上述内容变成一个整洁的循环,因为我非常清楚它是多么业余(我是 Python 新手)! Thank you!谢谢!

If you need references to all rand objects:如果您需要引用所有rand对象:

rand_list = [np.zeros((44100, 1)) for _ in range(10)]
stim_list = [rand_stim_seconds(r) for r in rand_list]

If you only need stim references:如果您只需要stim参考:

stim_list = [rand_stim_seconds(np.zeros((44100, 1))) for _ in range(10)]

What was previously stim1 is now stim[0] and so on.以前的stim1现在是stim[0]等等。 I am using list comprehension as it is the shortest to write.我正在使用列表理解,因为它是最短的。

The following code should do the trick:以下代码应该可以解决问题:

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))

you can them access any on your stimulation arrays by their index as stim[0] for the first element您可以通过他们的索引作为第一个元素的stim[0]访问您的刺激 arrays 上的任何内容

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM