我有一个4D数组,其尺寸如下:
In [3]: flrev.shape
Out[3]: (9, 12, 143, 144)
从这个数组(flrev),我想创建一个尺寸为(9,12,20306)的3D数组(dyn)。
在以下情况下将生成此数组:
from flrev(:,:,0,:)我只取第一个值flrev(:,:,0,0)
from flrev [:,:,1:-1 ,:]我取所有值,所以flrev [:,:,1:-1,:]
from fl [ff,ll,-1 ,:]我只取第一个值flrev [ff,ll,-1,0]
我试图通过循环并使用np.ravel函数来做到这一点:
phys = np.empty(([flrev.shape[0],flrev.shape[1],143*142]))
for ff in range(flrev.shape[0]):
for ll in range(flrev.shape[1]):
for ss in range(flrev.shape[2]):
for kk in range(flrev.shape[3]):
if flrev[ff,ll,ss,kk] == flrev[ff,ll,0,kk]:
phys[ff,ll,:] = np.ravel(flrev[ff,ll,0,0])
elif flrev[ff,ll,ss,kk] == flrev[ff,ll,1:-1,kk]:
phys[ff,ll,:] = np.ravel(flrev[ff,ll,1:-1,:])
elif flrev[ff,ll,ss,kk] == flrev[ff,ll,-1,kk]:
phys[ff,ll,:] = np.ravel(flrev[ff,ll,-1,0])
我得到的错误是:
47 if flrev[ff,ll,ss,kk] == flrev[ff,ll,0,kk]:
48 phys[ff,ll,:] = np.ravel(flrev[ff,ll,0,0])
---> 49 elif flrev[ff,ll,ss,kk] == flrev[ff,ll,1:-1,kk]:
50 phys[ff,ll,:] = np.ravel(flrev[ff,ll,1:-1,:])
51 elif flrev[ff,ll,ss,kk] == flrev[ff,ll,-1,kk]:
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
任何有关如何执行此操作的想法将不胜感激。 预先感谢。