Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I have an expression with subscripted variables. I would like to replace all those terms with other symbols. For example,

$P_1 + P_2^3 \rightarrow P1 + P2^3$

I used the following code but it doesn't work

X = Subscript[P, 1] + Subscript[P, 3]^2 - I*Subscript[P, 5]
X /. {Subscript[P, n_] -> Pn}

If I replace Subscript[P, n_] with P[n] then it works (e.g. $P_1$ will be replaced with P[1]) but I don't want the bracket. I want P1 instead. Is there any way to solve this?

share|improve this question
add comment

2 Answers 2

up vote 7 down vote accepted

Instead of Pn use Symbol["P"<>ToString[n]]

share|improve this answer
add comment
X /. Subscript[x_, y_] :> Symbol[StringJoin@(ToString /@ {x, y})]
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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