Why not vectorizing this loop by creating a index mask?