3
$\begingroup$

I simply want to be able to assign the subscript from something like Subscript[x, 1] to a new variable. How should I do this?

$\endgroup$
1
  • $\begingroup$ The question is not really clear. An example would help. $\endgroup$
    – wolfies
    Aug 1, 2021 at 23:24

2 Answers 2

5
$\begingroup$

If you have a variable

y=Subscript[x, 1]; 

then to extract the index it suffices to do

y[[2]] 

which in this case returns 1 as expected.

For more than one indices, this works

In[1]:= y=Subscript[x,1,2,3]
Out[1]= Subscript[x,1,2,3]

In[2]:= y[[2;;-1]]/.Subscript->List
Out[2]= {1,2,3}
$\endgroup$
1
$\begingroup$

For y with any number of subscripts ,

Rest[List @@ y]

also works. For instance

y = Subscript[x, 1, 2];
Rest[List @@ y]
(* {1, 2} *)

As a word of advice, use Subscript and the like for formatting expressions for display but not for symbolic computations, where it sometimes leads to problems.

$\endgroup$

Your Answer

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

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