1
$\begingroup$

I'm stuck in scripting in Mathematica, since I need to assign a value to "elements" of a list, named as a "subscripted variable" (the subscript is also a variable), but it seems that in Mathematica it's not that straight forward like for instance:

Subscript[a,i][[1]]=10;

Error: "a_i" in the part assignment is not a symbol.

I really need to use subscripted variables. I googled it and found some discussions regarding symbolizing subscripted variables ( "a_i" in this example), but I couldn't figure it out, what should I do exactly.

So, any clue about how to manage such thing is highly appreciated?

Sincerely, Marilla.

$\endgroup$
  • $\begingroup$ However urgent your need for subscripts, it is better to avoid them. Take a look at the related posts or search for subscript, and you will find many ways to manage that. $\endgroup$ – Yves Klett Mar 10 '16 at 10:02
  • $\begingroup$ As Yves says, it's normally a lot more effort than it's worth using subscripted variables (though it can be done). The most common workaround is to use a[i] = 10 $\endgroup$ – Quantum_Oli Mar 10 '16 at 10:06
  • $\begingroup$ Thank you guys for your response. The thing is that I really have to use these subscripted variables. It's easy to assign values to the whole "subscripted variable", however, if that variable is a list and you want to assign a value to one of its elements, the problem shows up. $\endgroup$ – Marilla Mar 10 '16 at 10:07
  • $\begingroup$ Why? Maybe be you do, but if we know your exact reason we might be able to suggest a better solution. Also see point 3 here: mathematica.stackexchange.com/a/18395/6588 $\endgroup$ – Quantum_Oli Mar 10 '16 at 10:08
  • $\begingroup$ Since there are many parameters that are related to each other and I need to save them all in a single subscripted variable. For instance, "a_i", saves the ID's of the nodes in the state "i" of a system, that have a packet in a network $\endgroup$ – Marilla Mar 10 '16 at 10:09
1
$\begingroup$

So first I assign a list to $a_i$,

Subscript[a, i] = {1, 2, 3}

but now if you want to change one of the elements, you get an error

Subscript[a, i][[1]] = 4;
Subscript[a, i]

During evaluation of In[61]:= Set::setps: Subscript[a, i] in the part assignment is not a symbol. >>

(* {1, 2, 3} *)

and $a_i$ hasn't changed. Here is a workaround,

Subscript[a, i] = ReplacePart[Subscript[a, i], 1 -> 4];
Subscript[a, i]
(* {4, 2, 3} *)

Sure, it's probably not memory efficient in that it creates a copy of the list, but so be it. I'll leave it to others to tell you why not to use subscripts. I don't use them personally, but frankly I think if they are so bad, then the system should stop allowing you to assign things to them. They are simply so like the items we use when doing derivations by hand that it is intuitive for the non-programmer to use them.

$\endgroup$
  • $\begingroup$ Thank you so much Jason! Yesss, it works pretty fine! That's realy great. $\endgroup$ – Marilla Mar 10 '16 at 12:11
  • $\begingroup$ @Marilla, glad to help. Welcome to the site, take the tour and come back if you need anything else $\endgroup$ – Jason B. Mar 10 '16 at 12:12
  • $\begingroup$ I'm really glad too; I was so overwhelmed for the last 2 days. $\endgroup$ – Marilla Mar 10 '16 at 12:14
  • $\begingroup$ Jason, may I ask that if the list is "nested" how it works; I tested it, but seems this doesn't work for assignments to the elements of a nested list. In the example you used, you assigned to the first element "1" value "4" ( 1 -> 4). But what if we want something like: [[1,1]] -> 4. Here is that part: Subscript[a,i] = ReplacePart[Subscript[a,i], Subscript[a,i][[1,1]] -> Subscript[b, j][[1]]]; $\endgroup$ – Marilla Mar 10 '16 at 14:46
  • $\begingroup$ Look at the documentation here, you would do something like ReplacePart[.....,{1,1}->newvalue] $\endgroup$ – Jason B. Mar 10 '16 at 14:48

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.