2

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}];
Alex
  • 1,235
  • 8
  • 23

3 Answers3

2

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\}$

Rod
  • 3,318
  • 2
  • 24
  • 40
  • yes!! but what about the rest? – Alex Jul 04 '13 at 01:09
  • @Alex You mean all other variables? Like "pressure", "hum", "lat", etc? – Rod Jul 04 '13 at 01:10
  • yes you wrote it just for "tem",that is alright.I need it for all for "pressure" etc – Alex Jul 04 '13 at 01:11
  • @Alex Please see my edit. – Rod Jul 04 '13 at 01:17
  • I need exactly what you got in the Subscripts of "X".like X"temp1",X"temp2",.... – Alex Jul 04 '13 at 01:21
  • 1
    @Alex Please see again... I've just edited the post. – Rod Jul 04 '13 at 01:21
  • @Alex Thanks for the accept! :-) – Rod Jul 04 '13 at 01:23
  • Thanks for you help and skills my friend. – Alex Jul 04 '13 at 01:24
  • One more thing what if the number of subscripts going to vary in different variables?Like "tem"= 8 and "lat"=12 – Alex Jul 04 '13 at 01:29
  • @Alex You could then table the variables individually... – Rod Jul 04 '13 at 01:30
  • Could you please write the code? – Alex Jul 04 '13 at 01:32
  • @Alex Please see it again... – Rod Jul 04 '13 at 01:42
  • 1
    How can I up vote it again???You have to be my guest for a drink in Finland then!! – Alex Jul 04 '13 at 01:47
  • @Alex LOL! I would love it! Meanwhile I keep drinking some caipirinhas here in Brazil! :-P (P.S. 2 upvotes are not possible, but maybe you could accept this question). – Rod Jul 04 '13 at 01:49
  • Rod your answer is great and you can see everybody liked that but you know this is still not what I needed.We can discuss about it later if you like.My knowledge in programming is next to zero but I have some knowledge in Graph theory.Can we talk about it in near future?Maybe 2-3 days later? – Alex Jul 04 '13 at 01:54
  • @Alex Of course! I thought you had "forgotten" the question, LOL ! Please tell me later what exactly you need! It will be a pleasure to help! – Rod Jul 04 '13 at 01:55
  • Rod how long have you been working with MMA and personally what is your opinion about Matlab and MMA? – Alex Jul 04 '13 at 01:56
  • @Alex I've written my master thesis in MMA 10 years ago... :-P But I'm still a newbie here... :-( – Rod Jul 04 '13 at 02:14
  • sorry Rod but it is much better version of your work.you still have my invitation to a drink "PLus" Mr. Goldberg!! – Alex Jul 04 '13 at 04:02
  • Can we write it in the very general term of Subscript[x, Subscript["a", "i"], Subscript["b", "j"], Subscript["c", "k"], Subscript["d", "f"], Subscript["...", "..."]]?? – Alex Jul 04 '13 at 19:08
2

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\}$

m_goldberg
  • 107,779
  • 16
  • 103
  • 257
  • nice job!!!Thats the way to do it. – Alex Jul 04 '13 at 03: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 04 '13 at 04: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 04 '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 04 '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 04 '13 at 19:14
2

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}
]
Mr.Wizard
  • 271,378
  • 34
  • 587
  • 1,371
  • thanks for the answer but you know that is not we discussing here if you look carefully. – Alex Jul 04 '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 04 '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 04 '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 04 '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 05 '13 at 12:58
  • thanks Mr Wizard.Actually I have got what I wanted originally from two previous answers; but your mentioned point raised my curiosity to broaden what I wanted.I have them already many thanks. – Alex Jul 05 '13 at 13:07