我有一个尺寸(Nt,Nx0,Ny0)的数组。对于轴0中的每个索引,我想在轴1和2中随机选择“矩形”。矩形具有固定大小Nx1乘Nx2,其中Nx1 此代码执行我想要执行的操作: 我觉得应该可以使用numpy索引来做到这一点,例如: 然而,这不起作用。如果没有for循环可以做到这一点吗? import numpy as np
for i in range(Nt):
x0ind = round(0.5*(Nx0-Nx1))+np.random.randint(-max_x, max_x)
x1ind = x0ind+Nx1
y0ind = round(0.5*(Ny0-Ny1))+np.random.randint(-max_y, max_y)
y1ind = y0ind+Ny1
ar1[i,:,:] = ar0[i,x0ind:x1ind,y0ind:y1ind]
import numpy as np
Nt = 100
Nx0 = 70
Ny0 = 70
Nx1 = 50
Ny1 = 50
max_x = 5
max_y = 5
ar0 = np.random.rand(Nt, Nx0, Ny0)
x_ind = np.random.randint(-max_x, max_x)
y_ind = np.random.randint(-max_y, max_y)
ar1 = ar0[:, x_ind:x_ind+Nx1, y_ind:y_ind+Ny1]