假设我有一个大小为 1024x1024x1x1x100 的 5D 数组。如何制作一个 1024x1024x100 的新数组?
如果您提前知道要保留哪些维度,则可以使用以下方法:
arr = arr[:, :, 1, 1, :]
但是我提前不知道哪些尺寸是多少并且我只想保留给定 bool 掩码的尺寸;像这样的东西...
arr2 = arr[(size(arr) .> 1)]
最佳答案
squeeze
函数是专门为删除长度为 1 的尺寸而定义的。手册中:
Base.squeeze — Function.
squeeze(A, dims)
Remove the dimensions specified by dims from array A. Elements of dims must be unique and within the range
1:ndims(A)
.size(A,i)
must equal 1 for alli
indims
.
为了“压缩”所有大小为 1 的维度(当它们事先未知时),我们需要找到它们并将它们放入元组中。这是通过 ((size(arr).==1)...)
完成的。所以结果是:
squeeze(a,(find(size(a).==1)...))
关于arrays - 删除长度为 1 的 Julia 数组维度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46289554/