Posts

Showing posts from January 3, 2019

Fastest way to go from linear index to grid index

Image
3 I'm sure this has been asked before but I'm interested in going from a position in a vector to the index in the grid version of the vector with given strides, for example, say I have the vector: vec = {58, 94, 19, 68, 54, 77, 1, 18, 49, 20, 90, 44, 91, 89, 15, 0, 60, 18, 19, 44, 87, 5, 8, 42, 51, 55, 87, 71, 83, 68, 53, 58, 27, 17, 8, 14, 33, 58, 86, 3, 91, 66, 3, 16, 98, 84, 72, 98, 9, 30, 90, 99, 15, 0, 82, 76, 86, 58, 77, 58}; And say I have strides {5, 4, 3} , the position 35 in the vector would correspond to the index {3, 4, 2} : vec[[35]] 4 ArrayReshape[vec, {5, 4, 3}][[3, 4, 2]] 4 How can I get this index fast and in a vectorized fashion because I will have potentially many positions to extract? list-manipulation performance-t