4
$\begingroup$

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.

$\endgroup$
3
  • 1
    $\begingroup$ test[[pos]]=test[[pos]] + 1 ? $\endgroup$ Aug 1, 2012 at 8:24
  • $\begingroup$ what do you want your final result to look like? $\endgroup$ Aug 1, 2012 at 8:29
  • $\begingroup$ test[[pos]] does not work because pos is a list. 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$
    – Pschoofs
    Aug 1, 2012 at 8:31

3 Answers 3

10
$\begingroup$

Try this:

test[[Sequence@@pos]]++
$\endgroup$
1
  • $\begingroup$ Works and returns the incremented list ! Thanks a lot ;) $\endgroup$
    – Pschoofs
    Aug 1, 2012 at 8:47
5
$\begingroup$

Why to go through the hustle defining pos manually and then using @@ ? With the same effort you can just put coordinates explicitly inside [[...]]

test[[4, 2, 3]]++
$\endgroup$
4
  • $\begingroup$ The idea is to do it dynamically for plenty of pos. And technically test can be of any dimension, and pos should be of the corresponding length. $\endgroup$
    – Pschoofs
    Aug 1, 2012 at 8:38
  • $\begingroup$ @Pschoofs Could you please show then the more general use/goal you are trying to achieve? $\endgroup$ Aug 1, 2012 at 8:39
  • $\begingroup$ Check the edit, I hope I could explain myself a little better ;) $\endgroup$
    – Pschoofs
    Aug 1, 2012 at 8:45
  • $\begingroup$ @Pschoofs thanks for the update. But I think if you can define and dynamically update pos as a separate statement, you can also easily do it inside [[...]] too ;-) $\endgroup$ Aug 1, 2012 at 8:53
4
$\begingroup$

If you "must" use Apply you can use it as

Increment[test[[##]]] & @@ pos
(* 37 *)

or

test[[##]]++ & @@ pos
(* 37 *)

which gives

test
(* {{{0, 1, 2, 3, 4}, {5, 6, 7, 8, 9}},
   {{10, 11, 12, 13, 14}, {15, 16, 17, 18, 19}}, 
   {{20, 21, 22, 23, 24}, {25, 26, 27, 28, 29}},
   {{30, 31, 32, 33, 34}, {35, 36, 38, 38, 39}}} *)
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.