I’m not sure that your description of what you want matches your example.
Could you explain how you get from the inputs:
M =
[[1 2 3]
 [4 5 6]
 [7 8 9]]
and
I = [1,2,1]

to your output,
[[1 2 3]
 [0 5 0]
 [0 0 0]]

? Should the 5 be there?