4

everyone! I hope to declare a variable with subscript as numbers, for example, $x_{10}$, $x_{11}$, $x_{01}$ as variables. Is it possible in mathematica? If so, how to implement it so that it treats $x_{10}$ as a variable? Thank you!

Ka Wa Yip
  • 431
  • 1
  • 3
  • 14

2 Answers2

2
Format[x[i_, j_]] :=
 Subscript[x, StringJoin[ToString /@ {i, j}]]

t = Total[{x[1, 0], x[1, 1], x[0, 1]}]

enter image description here

Solve[t == 1, x[0, 1]]

enter image description here

Bob Hanlon
  • 157,611
  • 7
  • 77
  • 198
2

Using the 'Notation' package, you can create real symbols from (not only!) subscripted variables:

Needs["Notation`"]

makesymbol[obj_]:=With[{},      
  If[NameQ@ToString@Unevaluated@obj,Remove@obj]; (* remove possibly existing symbol first *)
  Symbolize@ParsedBoxWrapper@ToBoxes@obj;]       (* then create the new symbol *)

With this function, you can create a symbol like so:

makesymbol[Subscript[x,"01"]]

Mathematica will now treat it the same as any other (simpler) symbol.

And the best part is:

  1. You have the same look&feel everywhere, both in your code and the generated output.
  2. It works for all kinds of notation, not only Subscript

I hope this might be of some help to you.

Jinxed
  • 3,753
  • 10
  • 24