indexing - Julia CartesianIndex 到整数 LinearIndex 的转换
问题描述
要将 CartesianIndex 转换为CartesianIndex(1,2)
LinearIndex,我可以使用 LinearIndeces 函数:
julia> a = rand(2,2)
2×2 Array{Float64,2}:
0.57097 0.0647051
0.767868 0.531104
julia> I = LinearIndices(a)
2×2 LinearIndices{2,Tuple{Base.OneTo{Int64},Base.OneTo{Int64}}}:
1 3
2 4
julia> I[CartesianIndex(1,2)]
3
但是,如何在3
不CartesianIndex(1,2)
构造数组实例的情况下获得 LinearIndex 整数a
?假设我知道 CartesianIndex 的范围,1:2, 1:2
.
解决方案
只需使用LinearIndices
一个轴元组(甚至只是一个维度大小的元组):
julia> LinearIndices((1:2,1:2))
2×2 LinearIndices{2,Tuple{UnitRange{Int64},UnitRange{Int64}}}:
1 3
2 4
julia> LinearIndices((1:2,1:2))[1,2]
3
推荐阅读
- python - 从 `torch` 或 `numpy` 向量创建 pytorch 张量
- gcc - 当线程清理程序报告数据竞争时如何添加断点?
- ios - Xcode 11 Simulator - 如何自定义模拟设备分辨率
- ios - 尝试在 UIImagePickerController didFinishPickingMediaWithInfo 回调中移动文件失败并出现“您没有权限”错误
- python - 使用 os.system 或 subprocess.Popopen 启动的软件在 python 关闭时关闭
- server - Netty 中的 ReplayingDecoder 在每次迭代中读取 10240 字节
- python - 如何适合第一个标签matplolib python的第二个标签底部
- cmd - 无法从 cmd 在 Documents 中找到文件
- django - 大型数据集的 Graphene-python 性能问题
- javascript - 我无法序列化用户,我不明白发生了什么