2

How do I assign a value, say 7 or "t", to a[1, 2][[3]]?

a[1, 2][[3]] = 7

and

a[1, 2][[3]] = "t"

both give

Set::setps: a[1,2] in the part assignment is not a symbol

I have looked here and here, but both questions ask more complicated questions and I'm afraid I'm none the wiser.

  • Why not just assign the value to a[1,2][3]? – Lukas Lang Sep 09 '17 at 18:50
  • 1
    Perhaps a[1, 2] = MapAt[7 &, a[1, 2], {3}] or a[1,2] = ReplacePart[a[1, 2], 3 -> 7]? – kglr Sep 09 '17 at 18:55
  • @Mathe172 - Thanks. That works. I thought "a[1,2][[3]]" was the correct way to refer to part 3 of a[1,2]. What is the difference? –  Sep 09 '17 at 18:56
  • 3
    You can't do this. Part assignments are not supported for indexed variables. This is a fundamental limitation, there is no way around it, if you want to keep assignments as O(1) operations. Note that the suggestions which use things like MapAt or RepalcePart will reassign entire list. Note also that a[1,2][3] is a completely different thing - it is a nested indexed variable, while a[1,2][[3]] is a part 3 of the value of indexed variable a[1,2]. – Leonid Shifrin Sep 09 '17 at 18:56
  • 1
    Fundamentally, indexed variables are stored in a kind of hash table (DownValues or SubValues), which are rule-based. This is why this kind of complex part assignments are not allowed there. You can use an association instead, where you will be able to do something similar to what you want. – Leonid Shifrin Sep 09 '17 at 19:01
  • 1
    It's not really clear what you want to achieve. Can you give a simple example where you show what the expected result (or effect) is? I suspect that everyone in the comments above misunderstood what you wanted, but I'm not sure. – Szabolcs Sep 09 '17 at 19:02
  • Thanks, @LeonidShifrin. I am a little out of my depth and will have to study what that difference is. I need an array (in the old sense; I'm not sure whether this is the correct word in Mathematica) with three dimensions, sizes 999, 99, and 6. Perhaps I'd be better using a[1,2,3]=7? –  Sep 09 '17 at 19:04
  • For a 3 dimensional array, you can reassign items using a[[1,2,3]]=7 - is this what you're after? – Lukas Lang Sep 09 '17 at 19:07
  • The lesson that's coming across is "if you want a 3D array, treat each dimension as the same kind of item fom the outset". a[1,2,3]=7 appears to do the trick. a[[1,2,3]]=7 gives Set::noval: Symbol a in part assignment does not have an immediate value. –  Sep 09 '17 at 19:11
  • 3
    When you define a[1,2,3] = 7, you don't define an array. Instead, you define an indexed variable. They are different objects. To define an array, you need to use List, like a = {{{1,2,3}, {4,5,6}}, {{7,8,9},{10,11,12}}}, which is a 3D array. Then you can do a[[1,2,3]] = 7, and that would replace the number 6 in that array by 7. But you must have an array stored in some variable first, to make such part assignments. – Leonid Shifrin Sep 09 '17 at 19:15
  • I read you question and the comments like you want the last part of Leonid's last comment: make a a List with three levels. – Marius Ladegård Meyer Sep 09 '17 at 19:53
  • Reading this, I think an indexed variable will meet my needs. –  Sep 09 '17 at 21:05
  • @ruffle is this enough for an answer? https://mathematica.stackexchange.com/q/148387/5478 – Kuba Sep 13 '17 at 14:53

0 Answers0