If I want to set element (i,j) of two-dimensional array X to 0, I can write X[[i,j]] = 0. My question is how can I achieve this if I have the indices i and j in a list.

If k = {i,j}, writing X[[k]] = 0 does not have the same effect, because it changes the ith and jth elements to zero separately. There are some obvious work-arounds. I could write
X[[k[[1]],k[[2]]]] = 0,
but that is both clunky and doesn't generalise automatically to higher-dimensional matrices. I could also write
X = ReplacePart[X, {k->0}],
but this is also clunky, and it wastes memory by creating another copy of X.

Is there an alternative approach?

  • 3
    $\begingroup$ x[[Sequence@@k]]=0; $\endgroup$ – N.J.Evans Oct 20 '17 at 12:09
  • $\begingroup$ Ah, of course---thank you! $\endgroup$ – Ivan Oct 20 '17 at 12:12
  • $\begingroup$ I may have been too hastily with closing as you need something more than expression manipulation. Let me know if you diagree with closing. $\endgroup$ – Kuba Oct 20 '17 at 14:09