3
$\begingroup$

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?

$\endgroup$

2 Answers 2

9
$\begingroup$

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

$\endgroup$
4
$\begingroup$
X /. Subscript[x_, y_] :> Symbol[StringJoin@(ToString /@ {x, y})]
$\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.