是否有一种优雅而简短的方法可以在一个定义中一次性编写下面编写的函数?这样该函数就可以对具有灵活维数的数组进行操作,并:根据需要添加 s 吗?理想情况下,它也具有N尺寸。
ind = 1:5
view_something(A::AbstractArray{T,1}, ind) where {T} = view(A, ind)
view_something(A::AbstractArray{T,2}, ind) where {T} = view(A, :, ind)
view_something(A::AbstractArray{T,3}, ind) where {T} = view(A, :, :, ind)
view_something(rand(10,10,10), ind)
view_something(rand(10,10), ind)
view_something(rand(10), ind)
Run Code Online (Sandbox Code Playgroud)
我注意到人们可以调用Colon运算符并将 func args 组装在这样的向量中[Colon(),Colon(),...],这是一种可行的方法还是还有其他首选方法?
eachslice(A, dims = d)给你一个迭代器,其中所有其他维度都是:
selectdim(A, d, ind)为您提供个人索引。
从帮助提示中?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.
See also: eachslice.
Run Code Online (Sandbox Code Playgroud)
和?eachslice:
eachslice(A::AbstractArray; dims)
Create a generator that iterates over dimensions dims of A, returning views that select all the data from the other
dimensions in A.
Only a single dimension in dims is currently supported. Equivalent to (view(A,:,:,...,i,:,: ...)) for i in axes(A,
dims)), where i is in position dims.
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
92 次 |
| 最近记录: |