3
$\begingroup$

I know this is a simple question, but I am really new to Mathematica.

If I have elements a = {1, 2} and b = {2, 3}, I can simply type

S = {{a}, {b}}

and get a set formed by 1-line matrices, a and b. But if I have 73 numbered elements,like w1, w2, ..., w73, this would take a while. With

S = Table[Symbol["w" <> ToString[i]], {i, 1, 73}]

I don't get exactly what I want. The output is like {w1, ..., w73} instead of {{w1}, ..., {w73}}.

$\endgroup$
2
$\begingroup$
S=Table[Symbol["w" <> ToString[i]], {i, 1, 73}];
S = List /@ S;
S // Short

{{w1}, {w2}, {w3}, {w4}, {w5}, {w6}, << 61 >>, {w68}, {w69}, {w70}, {w71}, {w72}, {w73}}

Alternatively,

S=Table[{Symbol["w" <> ToString[i]]}, {i, 1, 73}];
S // Short

{{w1}, {w2}, {w3}, {w4}, {w5}, {w6}, << 61 >>, {w68}, {w69}, {w70}, {w71}, {w72}, {w73}}

$\endgroup$
1
$\begingroup$

There are probably much better ways of handling your data than to define 73 different variables with names like w1, w2, ... w73. For example, if you define them as either a function or an array, then you can index into them and combine them in simple ways. Here's an example where w[1] and w[2] and etc are defined:

w[n_] := {n, n^2};

I'll just define them this way, but you could have them read in as data or created them in any fashion (just as you would have previously defined w1, w2, ... w73). Now you can make your output:

Table[w[i], {i, 1, 5}]
{{1, 1}, {2, 4}, {3, 9}, {4, 16}, {5, 25}}

or use Map:

w[#] & /@ Range[5]

to get the same output. If you wish another level of brackets, this can be accomplished with Table[{w[i]}, {i, 1, 5}] or with {w[#]} & /@ Range[5].

$\endgroup$
  • $\begingroup$ +1 for the tip ;-) $\endgroup$ – Filburt Sep 28 '17 at 22:43
  • $\begingroup$ The output is not what I was looking for. The output should be {{{1,1}}, {{2,4}},{{3,9}},{{4,16}},{{5,25}}}. Can you change your code to get this? $\endgroup$ – Filburt Sep 29 '17 at 2:00
  • $\begingroup$ Yest, we can just write Table[{w[i]}, {i, 1, 5}] =D $\endgroup$ – Filburt Sep 29 '17 at 2:13
1
$\begingroup$
fun[n_Integer?Positive] := Thread[{#, #^2} &[Range[1, n]]] 

Thread is a faster option than Table or Map in this case

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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