Re: Adding interpolated values to a list
- To: mathgroup at smc.vnet.net
- Subject: [mg20986] Re: [mg20961] Adding interpolated values to a list
- From: Hartmut Wolf <hwolf at debis.com>
- Date: Thu, 2 Dec 1999 21:41:03 -0500 (EST)
- Organization: debis Systemhaus
- References: <199912010650.BAA07690@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
Steve schrieb: > > I have recieved a large quantity of x, y ordered pairs from a > colleague for which I need to compute interpolated values and then add > these values (in proper sequence) to a list. A very simplified > description of this follows. > > X={2,4,6,8,10} > Y={0,10,12,2,4} > > Xnew={2,3,4,5,6,7,8,9,10} > Ynew={0,5,10,11,12,7,2,3,4} > > X and Y are the original ordered pairs sent to me. > > Xnew is the new x vector that I would create (using Range). > Ynew is the new y vector which corresponds to Xnew. > > Note that the ordered pairs (2,0), (4,10), (6,12), (8,2), and (10,4) > are maintained in the new lists. Additionally, the linearly > interpolated values are also included in Ynew so that a plot of the > original data will lie on top of a plot of the new data when plotted > on the same graph. > Hello Steve, taking your example: X = {2, 4, 6, 8, 10}; Y = {0, 10, 12, 2, 4}; cl = Transpose[{X, Y}] Out[5]= {{2, 0}, {4, 10}, {6, 12}, {8, 2}, {10, 4}} then with intp[___, {x1_, y1_}, s : {x2_, y2_}] := Sequence[{(x2 - x1)/2 + x1, (y2 - y1)/2 + y1}, s] FoldList[intp, First[cl], Rest[cl]] Out[19]= {{2, 0}, {3, 5}, {4, 10}, {5, 11}, {6, 12}, {7, 7}, {8, 2}, {9, 3}, {10, 4}} and Transpose[%][[2]] Out[20]= {0, 5, 10, 11, 12, 7, 2, 3, 4} gives your Ynew (just in case the result list was not convenient). Performance: xl = NestList[(# + Random[] &), 0., 1000]; yl = NestList[(# + Random[] &), 0., 1000]; Length[yl] Out[25]= 1001 cl = Transpose[{xl, yl}]; (rl = FoldList[intp, First[cl], Rest[cl]]); // Timing Out[28]= {0.52 Second, Null} Length[rl] Out[29]= 2001 However, since your x-es are all trivial you need only deal with the y-s and save half of the time: Append[Flatten[Apply[{#1, (#2 - #1)/2 + #1} &, Partition[yl, 2, 1], {1}]], Last[yl]]; // Timing Out[42]= {0.241 Second, Null} With[{Y = yl}, Append[Flatten@ Transpose[{Drop[Y, -1], Apply[(#2 - #1)/2 + #1 &, Partition[Y, 2, 1], {1}]}], Last[Y]]]; // Timing Out[43]= {0.23 Second, Null} Kind regards, Hartmut