Re: fast way of appending x zeros to a list?
- To: mathgroup at smc.vnet.net
- Subject: [mg79119] Re: fast way of appending x zeros to a list?
- From: Peter Pein <petsie at dordos.net>
- Date: Thu, 19 Jul 2007 03:27:54 -0400 (EDT)
- References: <f7ke8n$508$1@smc.vnet.net>
kristoph schrieb:
> Dear all,
>
> I'm looking for a fast way to append 0's to a list. Unfortunately, I
> append a variable number of 0's to a list which is generated by a
> For[...]-loop. Therefore, PadLeft[...] is not variable enough to
> append exactly x zeros to the list.
>
Then use PadRight[]! At the moment I use: Do[PrependTo[data,0],{K}]
where the K depends on
> certain factors.
>
> In case you have an idea to append exactly x zeros to the left quickly
> I would be more that happy since I heard that the PrependTo[...] and
> AppendTo[...] functions are very slow.
>
> Thanks a lot.
>
>
Hi Kristoph,
PadRight works perfectly (in Version 5.2):
In[1]:=
Append0 = PadRight[#1, Length[#1] + #2] & ;
to append five zeros to {1,2,3} and {1,2,3,4,5} resp. do:
In[2]:=
(Append0[Range[#1], 5] & ) /@ {3, 5}
Out[2]=
{{1, 2, 3, 0, 0, 0, 0, 0}, {1, 2, 3, 4, 5, 0, 0, 0, 0, 0}}
or did I misunderstand you?
Peter