Hello,
I’m working with matrices in such a way that I want to be able to access values by both the Cartesian and linear Indices. I want to be able to map any given linear index to its Cartesian counterpart and vice versa.
This is the best I have so far:
> a = rand(3,3)
3×3 Matrix{Float64}:
0.806 0.512 0.657
0.278 0.313 0.266
0.793 0.707 0.720
> for (index, value) in enumerate(CartesianIndices(a))
println(index, ", ", Tuple(value), ", ", a[index])
end
1, (1, 1), 0.806
2, (2, 1), 0.278
3, (3, 1), 0.793
4, (1, 2), 0.512
5, (2, 2), 0.313
6, (3, 2), 0.707
7, (1, 3), 0.657
8, (2, 3), 0.266
9, (3, 3), 0.720
The loop iterates over the matrix using its Cartesian indices, and enumerate applies a label to each element which happens to correspond to the linear index. I can save this map as an array of tuples, which would work, but it seems really clumsy. And I’m sure there is a built-in function that does what I need, I just haven’t been able to google exactly the right phrase yet.
Thanks in advance