[Jprogramming] replacing missing values with closest neighbor
Lettow, Kenneth
KLettow at ThomasNet.com
Fri Sep 30 01:02:47 HKT 2011
Raul & Thomas,
Thank you very much for your help. I wound up using Raul's brute force approach, which worked perfect for my small set of data (~1500 elements with about 500 missing values).
I did notice that Raul's explicit version gives a different answer from the brute force version. The difference is that the explicit version substituted the larger existing value for a missing value that fell exactly between 2 existing values.
nearValBrute=: ({."1 {~ |: i."_1 <./)@:|@(-/ 1 i. at + >./)
inds=: [: i. 1 + {:
poss=: (,{.) {~ _1 0 +/ ] I. inds
nearValOrdered =: |:@poss {~"_1 [: (|: i."_1 <./) inds |@-"1 poss
nearValExplicit=: 3 :0
inds=. i. 1 + {: y
poss=. (y,{.y) {~ 0 _1 +/ y I. inds
(|:poss) {~"_1 (|: i."_1 <./) |inds -"1 poss
)
(nearValBrute,nearValOrdered,:nearValExplicit) 1 2 5 9
1 1 2 2 5 5 5 5 9 9
1 1 2 2 5 5 5 5 9 9
1 1 2 2 5 5 5 9 9 9
Thanks again.
Ken
-----Original Message-----
From: programming-bounces at jsoftware.com [mailto:programming-bounces at jsoftware.com] On Behalf Of Raul Miller
Sent: Thursday, September 29, 2011 8:59 AM
To: Programming forum
Subject: Re: [Jprogramming] replacing missing values with closest neighbor
On Thu, Sep 29, 2011 at 6:18 AM, Raul Miller <rauldmiller at gmail.com> wrote:
> On Wed, Sep 28, 2011 at 10:44 PM, Lettow, Kenneth <KLettow at thomasnet.com> wrote:
>> sa=: 1 2 5 9
> ...
>> NB. nearest value in sa. The desired result should be:
>> desiredresult=: 1 1 2 2 5 5 5 5 9 9
>
> Here's a brute force approach:
>
> ({."1 {~ |: i."_1 <./)@:|@(-/ 1 i. at + >./) 1 2 5 9
> 1 1 2 2 5 5 5 5 9 9
Here's a version that relies on the data being ordered:
inds=: [: i. 1 + {:
poss=: (,{.) {~ _1 0 +/ ] I. inds
nearVal=: |:@poss {~"_1 [: (|: i."_1 <./) inds |@-"1 poss
nearVal 1 2 5 9
1 1 2 2 5 5 5 5 9 9
Or, an explicit version (which, in effect, caches the function results
within an execution):
nearVal=:3 :0
inds=. i. 1 + {: y
poss=. (y,{.y) {~ 0 _1 +/ y I. inds
(|:poss) {~"_1 (|: i."_1 <./) |inds -"1 poss
)
--
Raul
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm
More information about the Programming
mailing list