1
$\begingroup$

I want to set the value of an item in a list. The code that does this has to work regardless of the depth to the list.

For instance set the value of r[[1,1,1,....,1,1]] to 9. Lets say the list called $r$ is $t$ dimensions deep.

This doesn't see to work:

Part @@ {r}~Join~Table[1, t] = 9

It gives the following error:

Tag Apply in Part@@{...some numbers...} is Protected.

Is there some way to make it work? I tried a few things with Hold and Release but it didn't work.

$\endgroup$
2
  • 2
    $\begingroup$ r[[Sequence @@ Table[1, {t}]]] = 9 $\endgroup$
    – march
    May 4, 2016 at 15:26
  • $\begingroup$ Welcome to Mathematica.SE! I suggest the following: 1) As you receive help, try to give it too, by answering questions in your area of expertise. 2) Take the tour! 3) When you see good questions and answers, vote them up by clicking the gray triangles, because the credibility of the system is based on the reputation gained by users sharing their knowledge. Also, please remember to accept the answer, if any, that solves your problem, by clicking the checkmark sign! $\endgroup$
    – Michael E2
    May 4, 2016 at 15:31

1 Answer 1

3
$\begingroup$

Use ReplacePart

r = ReplacePart[r, Table[1, t] -> 9];
r[[1, 1, 1, 1, 1, 1, 1, 1, 1]]
$\endgroup$
1
  • $\begingroup$ Thanks, ReplacePart works flawlessly. $\endgroup$
    – CubeSy
    May 4, 2016 at 16:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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