CartesianIndices从起始和结束元组中创建

huangapple go评论53阅读模式
英文:

CartesianIndices from start and end tuples

问题

我想使用起始和结束元组构建CartesianIndices。例如,我想使用start = (2,3)end = (4,5)构建CartesianIndices((2:4, 3:5))

CartesianIndices没有为此提供构造函数。是否有简单的替代方法?

英文:

I want to construct CartesianIndices using start and end tuples. For example, I want to construct CartesianIndices((2:4, 3:5)) given start = (2,3) and end = (4,5).

CartesianIndices provides no constructor for this. Is there an easy alternative?

答案1

得分: 1

是的,有的:

julia> CartesianIndices(UnitRange.((2,3), (4,5))) |> collect
3×3 Matrix{CartesianIndex{2}}:
 CartesianIndex(2, 3)  CartesianIndex(2, 4)  CartesianIndex(2, 5)
 CartesianIndex(3, 3)  CartesianIndex(3, 4)  CartesianIndex(3, 5)
 CartesianIndex(4, 3)  CartesianIndex(4, 4)  CartesianIndex(4, 5)
英文:

Yes, there is:

julia> CartesianIndices(UnitRange.((2,3), (4,5))) |> collect
3×3 Matrix{CartesianIndex{2}}:
 CartesianIndex(2, 3)  CartesianIndex(2, 4)  CartesianIndex(2, 5)
 CartesianIndex(3, 3)  CartesianIndex(3, 4)  CartesianIndex(3, 5)
 CartesianIndex(4, 3)  CartesianIndex(4, 4)  CartesianIndex(4, 5)

huangapple
  • 本文由 发表于 2023年7月3日 10:49:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/76601589.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定