我正在使用Julia1.6
这里,X
是一个D
阶多维数组。如何在X
的d
轴上从i
切片到j
?
这里是D=6
和d=4
的例子。
X = rand(3,5,6,6,5,6)
Y = X[:,:,:,i:j,:,:]
CCD_ 10和CCD_。
如果只需要在单个轴上切片,请使用内置的selectdim(A, dim, index)
,例如selectdim(X, 4, i:j)
。
如果一次需要对多个轴进行切片,可以通过首先创建一个所有Colon
的数组,然后用指定的索引填充指定的维度来构建对数组进行索引的数组。
function selectdims(A, dims, indices)
indexer = repeat(Any[:], ndims(A))
for (dim, index) in zip(dims, indices)
indexer[dim] = index
end
return A[indexer...]
end
您可以使用内置的函数selectdim
help?> selectdim
search: selectdim
selectdim(A, d::Integer, i)
Return a view of all the data of A where the index for dimension d equals i.
Equivalent to view(A,:,:,...,i,:,:,...) where i is in position d.
Examples
≡≡≡≡≡≡≡≡≡≡
julia> A = [1 2 3 4; 5 6 7 8]
2×4 Matrix{Int64}:
1 2 3 4
5 6 7 8
julia> selectdim(A, 2, 3)
2-element view(::Matrix{Int64}, :, 3) with eltype Int64:
3
7
它将被使用类似于:
julia> a = rand(10,10,10,10);
julia> selectedaxis = 5
5
julia> indices = 1:2
1:2
julia> selectdim(a,selectedaxis,indices)
注意,在文档示例中,i是一个整数,但也可以使用i:j
形式的范围。
idx = ntuple( l -> l==d ? (i:j) : (:), D)
Y = X[idx...]