Access arrays in memory order, along columns

You can also use eachindex or CartesianIndices or pairs for this sort of thing, depending on the calculations you want to perform. They will choose the right order for you. You shouldn’t have to think about this too often.

In most cases, do

for i in eachindex(a)
    a[i] = ...
end

If you need the actual indices, like in your example,

for ind in CartesianIndices(a)
    a[ind] = sum(Tuple(ind))
end

These work for arrays of arbitrary dimensions.

There are several indexing functions that make working with arrays much nicer than in Matlab.

6 Likes