在Julia中对多维数组进行子采样

t_d*_*lan 2 multidimensional-array julia

我有一个大小为330x534x223的数组,其中包含值的分布,如3d图像.它的大小对我来说太大了,所以我想在每个维度上重新采样20倍.

有没有可想到的方法呢?我试过检查文档

谢谢

tho*_*oly 9

如果您正在考虑"缩略图",那么只考虑每个第20个元素可能并不令人满意.对于同时平滑和具有非常好的性能的子样本的东西,我建议restrict从JuliaImages(减少2倍,你可以重复调用它).


tim*_*tim 5

这个怎么样?

julia> subsample(a, n) = getindex(a, (indices(a,i)[1:n:end] for i=1:ndims(a))...)
subsample (generic function with 1 method)

julia> a = reshape(1:10^6, (100,100,100));

julia> subsample(a, 50)
2×2×2 Array{Int64,3}:
[:, :, 1] =
  1  5001
 51  5051

[:, :, 2] =
 500001  505001
 500051  505051
Run Code Online (Sandbox Code Playgroud)

这也适用于具有非常规索引的数组OffsetArrays.

对于大型数组,与使用范围的直接索引相比,此实现的开销可以忽略不计.

编辑: 类型稳定版本:

subinds(n, inds) = (first(inds)[1:n:end], subinds(n, Base.tail(inds))...)
subinds(n, ::Tuple{}) = ()
subsample(a, n) = getindex(a, subinds(n, indices(a))...)
Run Code Online (Sandbox Code Playgroud)