MathGroup Archive 2012

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: ListInterpolate and missing values

  • To: mathgroup at smc.vnet.net
  • Subject: [mg124867] Re: ListInterpolate and missing values
  • From: Ulrich Arndt <ulrich.arndt at data2knowledge.de>
  • Date: Fri, 10 Feb 2012 05:52:13 -0500 (EST)
  • Delivered-to: l-mathgroup@mail-archive0.wolfram.com
  • References: <201202091034.FAA18221@smc.vnet.net>

Hi,

I didn't checked the ListInterpolate as it was not obvious to me how this can help.

Maybe below function will do sufficient enough - test case of 1 mio element list in < 2 sec.

I added a constrained to the function that the first and the last element should not be null as no rule was given for this case...

Ulrich

x={1,2,10,,12,2,6,6,,,,12,22};

Clear[specialInterpole];

=
specialInterpole[x_List/;NumericQ[x[[1]]]==True&&NumericQ[x[[-1]]]== True]:= Module[{out,p,t,vals,diff,len,increment,newvals},
p = Position[x,Null]//Flatten;
t = Split[p,#2==#1+1&];
If[Length[p]>0,
vals = Map[{x[[Min[#]-1]],x[[Max[#]+1]]}&,t];
diff = Map[#[[2]]-#[[1]]&,vals];
len = Map[Length,t];
increment = Map[#[[1]]/#[[2]]&,Transpose[{diff,len+1}]];
newvals =  vals[[All,1]]+Map[Range,len] * increment;
out = x;
out[[Flatten[t]]]=Flatten[newvals];
];
out];


specialInterpole[x]
Out[156]= {1,2,10,11,12,2,6,6,15/2,9,21/2,12,22}

specialInterpole[{1,,,,,,,,,10}]
Out[157]= {1,2,3,4,5,6,7,8,9,10}

specialInterpole[{,1,2}]
Out[158]= specialInterpole[{Null,1,2}]

specialInterpole[{1,1,}]
Out[159]= specialInterpole[{1,1,Null}]

specialInterpole[{1,,1}]
Out[160]= {1,1,1}

SeedRandom[1]
dim = 1000000
dat =Range[dim];
dat[[RandomSample[dat,dim/10]]]= ConstantArray[Null,dim/10];
dat[[1;;20]]
dat[[-1]]

Out[395]= 1000000
Out[398]= =
{1,Null,3,4,5,6,7,8,Null,Null,11,12,13,Null,Null,16,17,18,19,20}
Out[399]= 1000000

Timing[out = specialInterpole[dat];]
Out[400]= {1.49907,Null}

out==Range[dim]
Out[401]= True


--
www.data2knowledge.de

Am 09.02.2012 um 11:34 schrieb Mark Coleman:

> Hi,
>
> I'm looking for a way to linearly interpolate missing values in a
> large list of numbers such that the interpolated values reflect the
> size of the "gap" between non-missing values. For instance, say I have
> the following list:
>
> x={1,2,10,,12,2,6,6,,,12,22}
>
> The list x has two gaps. A gap of size "1" at x[4], and a gap a size
> "2" at x[10]-x[11]. I'm looking for a linear interpolation method such
> that
>
> x[4] = 11,
>
> x[10]=8, x[11]=10
>
> That is, each of the interpolated values is a constant increment
> between the two surrouding non-missing values. My actually problem has
> millions of such lists, where gaps are dispersed randomly (In thinking
> about his more, missing endpoints could also be interpolated by
> "extending" the line between the two succeeeding or preceeding
> points).
>
> I'm wondering if ListInterpolate can handle this sort of problem
> directly? If not, any help would be much appreciated.
>
> Cheers,
>
> Mark
>




  • Prev by Date: Re: The simple command x=0;Dynamic[x=x+1]
  • Next by Date: Re: How to call 'Clear' from within a function?
  • Previous by thread: Re: How to select only points which are inside a domain
  • Next by thread: Re: How to select only points which are inside a domain