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 know I can define

vars = Map[Subscript[x, #1] &, {"tem", "pressure", "hum", "lat", "alt", "space", "corner"}]

and

vars = Array[Subscript[x, #1] &, {8}]

But how can I add both kinds of subscripts in my "x"s to get something like x"tem""1", x"tem""2", and so on?

Based on the answer I got here, I tried to write general form in this way.I am quite sure there would be much better interactive way to do that.This is just starter version of mine:

Outer[Subscript[x, Subscript[#1, #2], Subscript[#3, #4], 
Subscript[#5, #6], Subscript[#7, #8]] &, {"a", "b"}, {1, 2, 3, 
4}, {"i", "j"}, {1, 2, 3, 4}, {"m", "n"}, {1, 2}, {"A", "B", "C", 
"D"}, {1, 2, 3, 4, 5, 6, 7}];
share|improve this question

3 Answers 3

up vote 2 down vote accepted

I take a somewhat different approach to defining a function to make subscripted variables in of the requested form. The algorithm doesn't differ substantially from the one used by Rod Lm; the difference lies in the way I use multiple function definitions, pattern matching, and destructuring of the formal argument sequences.

SetAttributes[fancySubscript, HoldFirst]
fancySubscript[var_Symbol, tag_String, index_Integer] /; 
    Not[ValueQ[var]] :=
  Subscript[var, tag <> ToString[index]]
fancySubscript[var_Symbol, tags : {_String ..}, index_Integer] /; 
    Not[ValueQ[var]] :=
  fancySubscript[var, #, index] & /@ tags
fancySubscript[var_Symbol, tags : {_String ..}, 
   indices : {_Integer ..}] /; Not[ValueQ[var]] :=
  fancySubscript[var, tags, #] & /@ indices

The condition Not[ValueQ[var] ensures that fancySubscript doesn't accept variables having values.

a = 42; fancySubscript[a, "tag", 2]

fancySubscript[a, "tag", 2]

The overloaded definitions ensure that fancySubscript accepts all the following forms:

fancySubscript[b, "tag", 2]

$b_{\text{tag2}}$

fancySubscript[b, {"foo", "bar", "baz"}, 2]

$\left\{b_{\text{foo2}},b_{\text{bar2}},b_{\text{baz2}}\right\}$

fancySubscript[b, {"foo", "bar", "baz"}, Range@3]

$\left( \begin{array}{ccc} b_{\text{foo1}} & b_{\text{bar1}} & b_{\text{baz1}} \\ b_{\text{foo2}} & b_{\text{bar2}} & b_{\text{baz2}} \\ b_{\text{foo3}} & b_{\text{bar3}} & b_{\text{baz3}} \\ \end{array} \right)$

Edit

To answer the question raised in a comment by Alex, it is easy to change fancySubscript to do double indexing, rather than concatenating the tags and indices. The only modification required is to redefine the first definition of fancySubscript as

fancySubscript[var_Symbol, tag_String, index_Integer] /; 
    Not[ValueQ[var]] := 
  Subscript[var, tag, index]

With this change, for example,

fancySubscript[b, {"foo", "bar", "baz"}, 2]

gives

$\left\{b_{\text{foo},2},b_{\text{bar},2},b_{\text{baz},2}\right\}$

share|improve this answer
    
nice job!!!Thats the way to do it. –  Alex Jul 4 '13 at 3:57
    
@Rod LM sorry Rod but it is much better version of your work.you still have my invitation to a drink "PLus" Mr. Goldberg!! –  Alex Jul 4 '13 at 4:01
    
how can we write it in very general form of Subscript[x, Subscript["a", "i"], Subscript["b", "j"], Subscript["c", "k"], Subscript["d", "f"], Subscript["...", "..."]] –  Alex Jul 4 '13 at 19:07
    
Are you changing your question to request that the tags and indices be separate subscripts?. I think that could be done by combining my answer with Mr.Wizard's. Because of 4th of July festivities, I don't have time to work it out today. If no one else does (unlikely), I look into it later. –  m_goldberg Jul 4 '13 at 19:12
    
yes I mean exactly that.Happy 4th July and wish you and your family really a good time there!!! –  Alex Jul 4 '13 at 19:14

Maybe like this?

names = {"tem", "pressure", "hum", "lat", "alt", "space", "corner", "snow"};
vars = Table[Subscript[x, names[[y]] <> ToString[#]], {y, 1, 8}] & /@ Range@8

$ \begin{array}{cccccccc} x_{\text{tem1}} & x_{\text{pressure1}} & x_{\text{hum1}} & x_{\text{lat1}} & x_{\text{alt1}} & x_{\text{space1}} & x_{\text{corner1}} & x_{\text{snow1}} \\ x_{\text{tem2}} & x_{\text{pressure2}} & x_{\text{hum2}} & x_{\text{lat2}} & x_{\text{alt2}} & x_{\text{space2}} & x_{\text{corner2}} & x_{\text{snow2}} \\ x_{\text{tem3}} & x_{\text{pressure3}} & x_{\text{hum3}} & x_{\text{lat3}} & x_{\text{alt3}} & x_{\text{space3}} & x_{\text{corner3}} & x_{\text{snow3}} \\ x_{\text{tem4}} & x_{\text{pressure4}} & x_{\text{hum4}} & x_{\text{lat4}} & x_{\text{alt4}} & x_{\text{space4}} & x_{\text{corner4}} & x_{\text{snow4}} \\ x_{\text{tem5}} & x_{\text{pressure5}} & x_{\text{hum5}} & x_{\text{lat5}} & x_{\text{alt5}} & x_{\text{space5}} & x_{\text{corner5}} & x_{\text{snow5}} \\ x_{\text{tem6}} & x_{\text{pressure6}} & x_{\text{hum6}} & x_{\text{lat6}} & x_{\text{alt6}} & x_{\text{space6}} & x_{\text{corner6}} & x_{\text{snow6}} \\ x_{\text{tem7}} & x_{\text{pressure7}} & x_{\text{hum7}} & x_{\text{lat7}} & x_{\text{alt7}} & x_{\text{space7}} & x_{\text{corner7}} & x_{\text{snow7}} \\ x_{\text{tem8}} & x_{\text{pressure8}} & x_{\text{hum8}} & x_{\text{lat8}} & x_{\text{alt8}} & x_{\text{space8}} & x_{\text{corner8}} & x_{\text{snow8}} \\ \end{array}$

EDITED

If you have a different range for each variable, you could rewrite your code like this:

makesubs[var_, range_] := Subscript[x, ToString[var] <> ToString[#]] & /@ Range@range

Now set your names list like this:

names = {{"tem", 3}, {"pressure", 2}, {"hum", 4}, {"lat", 2}, {"alt", 7}, {"space", 2},
 {"corner", 3}, {"snow", 4}};

Now use it!

makesubs[names[[#, 1]], names[[#, 2]]] & /@ Range@Length@names

$\left\{\left\{x_{\text{tem1}},x_{\text{tem2}},x_{\text{tem3}}\right\},\left\{x_{\text{pressure1}},x_{\text{pressure2}}\right\},\left\{x_{\text{hum1}},x_{\text{hum2}},x_{\text{hum3}},x_{\text{hum4}}\right\},\left\{x_{\text{lat1}},x_{\text{lat2}}\right\},\left\{x_{\text{alt1}},x_{\text{alt2}},x_{\text{alt3}},x_{\text{alt4}},x_{\text{alt5}},x_{\text{alt6}},x_{\text{alt7}}\right\},\left\{x_{\text{space1}},x_{\text{space2}}\right\},\left\{x_{\text{corner1}},x_{\text{corner2}},x_{\text{corner3}}\right\},\left\{x_{\text{snow1}},x_{\text{snow2}},x_{\text{snow3}},x_{\text{snow4}}\right\}\right\}$

share|improve this answer
    
yes!! but what about the rest? –  Alex Jul 4 '13 at 1:09
    
@Alex You mean all other variables? Like "pressure", "hum", "lat", etc? –  Rod Jul 4 '13 at 1:10
    
yes you wrote it just for "tem",that is alright.I need it for all for "pressure" etc –  Alex Jul 4 '13 at 1:11
1  
@Alex Please see again... I've just edited the post. –  Rod Jul 4 '13 at 1:21
1  
How can I up vote it again???You have to be my guest for a drink in Finland then!! –  Alex Jul 4 '13 at 1:47

Perhaps I'm missing some subtlety of the question but it seems to me that you could merely do:

MapIndexed[
 Subscript[x, #, #2[[1]]] &,
 {"tem", "pressure", "hum", "lat", "alt", "space", "corner"}
]

$\left\{x_{\text{tem},1},x_{\text{pressure},2},x_{\text{hum},3},x_{\text{lat},4},x_{\text{alt},5},x_{\text{space},6},x_{\text{corner},7}\right\}$


I failed to understand your original quesiton, but looking at the update here is another section of code that produces the same output. Whether or not it is better I don't know.

Outer[
  Subscript[x, ##] & @@ Subscript @@@ Partition[{##}, 2] &,
  {"a", "b"}, {1, 2, 3, 4}, {"i", "j"}, {1, 2, 3, 4}, {"m", "n"},
   {1, 2}, {"A", "B", "C", "D"}, {1, 2, 3, 4, 5, 6, 7}
]
share|improve this answer
    
thanks for the answer but you know that is not we discussing here if you look carefully. –  Alex Jul 4 '13 at 15:50
    
@Alex I couldn't figure out what you meant by "to get something like x"tem""1", x"tem""2", and so on?" Would MapIndexed[Subscript[x, Row@{#, #2[[1]]}] &, {"tem", "pressure", "hum", "lat", "alt", "space", "corner"}] serve your purpose? –  Mr.Wizard Jul 4 '13 at 16:05
    
I already had two brilliant answers to my question.Please look at the answers,that show what I want. –  Alex Jul 4 '13 at 16:14
    
Mr. Wizard could you please participate to broaden Mr. Goldberg code to very general form of Subscript[x, Subscript["a", "i"], Subscript["b", "j"], Subscript["c", "k"], Subscript["d", "f"], Subscript["...", "..."]]? –  Alex Jul 4 '13 at 19:05
    
@Alex I editing my answer with a small variation of the code presently in the question. I don't know if it helps, or if that's the kind of thing you're seeking. –  Mr.Wizard Jul 5 '13 at 12:58

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.