Re: nth differences
- To: mathgroup at smc.vnet.net
- Subject: [mg39900] Re: nth differences
- From: Jens-Peer Kuska <kuska at informatik.uni-leipzig.de>
- Date: Tue, 11 Mar 2003 02:36:10 -0500 (EST)
- Organization: Universitaet Leipzig
- References: <b4c9lo$nqg$1@smc.vnet.net>
- Reply-to: kuska at informatik.uni-leipzig.de
- Sender: owner-wri-mathgroup at wolfram.com
Hi,
NthDifference[lst_List, n_Integer] :=
Nest[Drop[# - RotateRight[#], 1] &, lst, n]
and
lst = {1, 2, 5, 7, 8, 9};
Table[NthDifference[lst, i], {i, 1, 3}]
gives
{{1, 3, 2, 1, 1}, {2, -1, -1, 0}, {-3, 0, 1}}
Regards
Jens
Zachary Turner wrote:
>
> Say I have a list of k integers and I want to produce a list containing the
> first differences? For example, given {1, 2, 5, 7, 8, 9} the first
> differences are {1, 3, 2, 1, 1}, and the second differences are {2, -1, -1,
> 0}, the third are {-3, 0, 1}, etc