I've been wanting to increment a nested list element of which I know the "coordinates". I thought of the following procedure, using Apply with Part.
test = Table[10 h + 5 i + j, {h, 0, 3}, {i, 0, 1}, {j, 0, 4}];
pos = {4, 2, 3};
Increment[test[[##]] & @@ pos];
Yet it seems that test[[##]] & @@ pos
is treated like a different variable. When I print
test[[##]] & @@ pos
test
I can see that test[[##]] & @@ pos
gives an incremented result, but test
itself is not changed. What would be a nice and efficient way of incrementing a nested list element, saving the new value of the whole list ?
Edit
So in the light of the first comments, let me say more about what I would like to achieve :
I have a nested list test
which can be of any depth. I have a list pos
whose length is equal to
Depth[test]-1
The goal is to increment the element at the position pos
in test
(I guess by using Apply
) and return the incremented nested list. My method below increments well but then does not store the new value in the list.
test[[pos]]=test[[pos]] + 1
? $\endgroup$test[[pos]]
does not work because pos is alist
. I would like my final result to be the same nested list but with one of the element (given by ´pos`) incremented by one. $\endgroup$