Re: "expanding a list"
- To: mathgroup at smc.vnet.net
- Subject: [mg57897] Re: "expanding a list"
- From: Jean-Marc Gulliet <jeanmarc.gulliet at 9online.fr>
- Date: Sun, 12 Jun 2005 04:34:18 -0400 (EDT)
- Organization: New York University
- References: <d8bd00$fj0$1@smc.vnet.net>
- Reply-to: jmg336 at nyu.edu
- Sender: owner-wri-mathgroup at wolfram.com
Mark Coleman wrote:
> Greetings,
>
> I'd like to construct a list as follows. Say I have a list of reals
> {x1,x2,x3,...xn}, and another list of integers {r,s}. I'd like to
> make a new list
>
> {{x1,x1,x1,...},{x2,x2,x2....},{x3,x3,x3,...},....,{xn,xn,xn,...}},
> where the first sublist is of length r, and the remaining sublists
> are of length s.
Hi Mark,
I think the following lines do the trick (first we create some data,
then we define a function called "listExpand"):
In[1]:=
data = {1.02, 5.7, 3.987, 4.212, 9.11}
Out[1]=
{1.02,5.7,3.987,4.212,9.11}
In[2]:=
listExpand[L_, r_, s_] := FlattenAt[{Table[L[[1]], {r}],
(Table[#1, {s}] & ) /@ Rest[L]}, 2];
In[3]:=
listExpand[data, 3, 5]
Out[3]=
{{1.02,1.02,1.02},{5.7,5.7,5.7,5.7,5.7},{3.987,3.987,
3.987,3.987,3.987},{4.212,4.212,4.212,4.212,4.212},{
9.11,9.11,9.11,9.11,9.11}}
Best regards,
/J.M.