如何在Python中使用嵌套的For循环填充Numpy 4D数组?

时间:2017-11-23 17:28:49

标签: python arrays loops numpy nested

我有一个4维数组,其中前3个维度是x,y,z位置,第4个维度是时间。

我必须为这个数组中的(每个体素)制作一个过滤器,然后从每个体素中减去每个过滤器,然后在名为data_4d_filt的新数组中填入新的过滤值!

我在找到嵌套的Forloop中正确的索引时遇到了麻烦,因为它是4D,我已经忘记了我的位置以及我在做什么!

我真的不知道我是应该索引我的Filter还是要填充data_4d_filt数组?

所以在下面的代码中我应该这样做:

Filter[i, ii, iii] = gaussian_filter(data_4d[i, ii, iii], 25)
data_4d_filt = data_4d - Filter

或者这个:

Filter[i] = gaussian_filter(data_4d[i], 25)
data_4d_filt = data_4d - Filter

或者这个:

Filter = gaussian_filter(data_4d, 25)
data_4d_filt[i, ii, iii] = data_4d[i, ii, iii] - Filter

任何人都可以帮我这个PLZ ???

data_4d = nib.load('unfiltered_data_ds.nii.gz').get_data()
print("Shape of the original 4D fMRI scan: %s" % (data_4d.shape,))

# Here, we pre-allocate a matrix of the same shape as data_4d, in which
# you need to store the filtered timeseries
data_4d_filt = np.zeros(data_4d.shape)

# Start loop across X-dimension
for i in range(data_4d.shape[0]):

# Start loop across Y-dimension
for ii in range(data_4d.shape[1]):

    # Start loop across Z-dimension
    for iii in range(data_4d.shape[2]):
        # Filter the timeseries for voxel at location X=i, Y=ii, Z=iii and store it
        # using an appropriate index in the pre-allocated variable data_4d_filt!



        # My CODE HERE
        Filter[i, ii, iii] = gaussian_filter(data_4d[i, ii, iii], 25)
        data_4d_filt = data_4d - Filter

0 个答案:

没有答案