(It is always dangerous to solve a problem for which Miller provided a
solution already.)
(#~ 2 -~/\ 0, >:@{: ,~ 2 <.@>:@-:@+/\ ]) 1 2 5 9
1 1 2 2 5 5 5 5 9 9
This works more general:
[A=: (] /:"1 {.) 8(?,:?@#)30
1 4 8 11 19 24 25 26
8 28 3 16 13 28 21 29
Giving the length, indexes and values, (30; A) is representing
({:A)({.A)} 30$_
_ 8 _ _ 28 _ _ _ 3 _ _ 16 _ _ _ _ _ _ _ 13 _ _ _ _ 28 21 29 _ _ _
({:@] #~ 2 -~/\ 0, [ ,~ 2 <.@>:@-:@+/\ {.@])&>/ 30 ; A
8 8 8 28 28 28 28 3 3 3 16 16 16 16 16 16 13 13 13 13 13 13 28 28 28 21 29
29 29 29
NB. Be aware, wrap around.
R.E. Boss
-----Oorspronkelijk bericht-----
Van: [email protected]
[mailto:[email protected]] Namens Raul Miller
Verzonden: donderdag 29 september 2011 20:15
Aan: Programming forum
Onderwerp: Re: [Jprogramming] replacing missing values with closest neighbor
On Thu, Sep 29, 2011 at 1:02 PM, Lettow, Kenneth <[email protected]>
wrote:
> 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.@+ >./)
>
> 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
Yes... I had pasted in an early copy of nearValExplicit
If you change
poss=. (y,{.y) {~ 0 _1 +/ y I. inds
to
poss=. (y,{.y) {~ _1 0 +/ y I. inds
it should be consistent with the other versions.
Sorry about that.
--
Raul
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm