1
|
14219 Views
|
4 Replies
|
7 Total Likes
View groups...
Share
GROUPS:

# How to get rid of the outermost curly brackets of a Table?

Posted 9 years ago
 Hi, say I have a table {a,b,c,d}  How can I get rid of the outermost brackets here, so that I have and expression of the form a,b,c,d  I would need this very often in order to construct a list of arguments, which a function should take. StringReplace does not do the job in most cases, since the elements a,b,c,d might be lists their selves, i.e. they might contain curly brackets as well. Thanks for help!
4 Replies
Sort By:
Posted 9 years ago
 And more to the point: In:= ranges = {{x, 1, 7, 2}, {y, 2, 8, 2}}; In:= Table[{x, y}, Sequence @@ ranges // Evaluate] Out= {{{1, 2}, {1, 4}, {1, 6}, {1, 8}}, {{3, 2}, {3, 4}, {3, 6}, {3, 8}}, {{5, 2}, {5, 4}, {5, 6}, {5, 8}}, {{7, 2}, {7, 4}, {7, 6}, {7, 8}}} ;-)
Posted 9 years ago
 Hi Gernot,In Mathematica, everything is an expression of some kind. The kind is identified by its Head. The Head of {a,b,c} is List. What Apply does is to replace the Head with something else. In this case, List is replaced by Sequence, which is the head of a sequence of items.However, some functions look at their arguments before evaluation, and will object. This can be remedied by forcing the evaluation. In:= (* f expects 3 arguments and passes them to g *) f[a_, b_, c_] := g[a, b, c] In:= f[a, b, c] Out= g[a, b, c] In:= (* this is returned unevaluated because there is no \ definition for f with 1 argument *) f[{a, b, c}] Out= f[{a, b, c}] In:= (* this works fine *) f[Sequence @@ {a, b, c}] Out= g[a, b, c] In:= (* so does this because f is willing to accept lists as the \ individual arguments *) f[{1, 2}, {3, 4}, {5, 6}] Out= g[{1, 2}, {3, 4}, {5, 6}] In:= (* and so does this *) f[Sequence @@ {{1, 2}, {3, 4}, {5, 6}}] Out= g[{1, 2}, {3, 4}, {5, 6}] In:= (* here is a built in function *) Sum[x^2, {x, 1, 3}] Out= 14 In:= (* this does not work because Sum looks at its arguments \ before evalauating *) Sum[x^2, Sequence @@ {{x, 1, 3}}] During evaluation of In:= Sum::vloc: The variable Sequence@@{{x,1,3}} cannot be localized so that it can be assigned to numerical values. >> Out= \!$$\*UnderscriptBox[\(\[Sum]$$, $$Sequence @@ {{x, 1, 3}}$$]\)x^2 In:= (* but if we force the evaluation, Sum is happy *) In:= Sum[x^2, Sequence @@ {{x, 1, 3}} // Evaluate] Out= 14 
Posted 9 years ago
 But this only works if i want to Apply the whole list as argument, right?What for example if I have something like Table[ something , {i1,n1},{i2,n2},{i3,n3}] I want to generate {i1,n1}},{i2,n2},{i3,n3} as a sequence, and use it as such.
Posted 9 years ago
 Apply ( @@ ) can be used to replace the head List with Sequence, which is the head for a sequence of items as used for arguments. In:= Range[1, 10, 1] Out= {1, 2, 3, 4, 5, 6, 7, 8, 9, 10} In:= Range[Sequence @@ {1, 10, 1}] Out= {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}