I have some matlab code that uses the old sub2ind way of getting the linear indices (this is a simplified version that gets at the essence of it):
row = [1, 2, 3]; # x-component of cartesian index column = [1, 2, 3]; # y-component of cartesian index sz = [3 3]; # size of the matrix ind = sub2ind(sz, row, column);
This spits out ind = [1, 5, 9]
Using the LinearIndices() and CartesianIndex() functions that Julia moved to I can implement it like this:
inds = LinearIndices((3, 3))[CartesianIndex.(row, col)]
But this somehow seems clunky. According to Pluto it is also slower that the deprecated sub2ind function:
Is there a better way to implement this?