Actually this highlights that using either 10 o. or | will both cause problems 
for non-complex data that includes negatives. The following remedies this

rescale=:3 :0
  1 rescale y
:
  'data xtrma'=. 2{. boxopen y
  'minX magX minY magY'=. (2{.x) ,&(-~/\@(<./ , >./)) |^:(16=3!:0) xtrma,,data
  minX+magX*(data-minY)%magY
)

However I think I'd probably prefer to format it like this for 
readability/maintainability.

rescale=:3 :0
  1 rescale y
:
  'data xtrmY'=. 2{. boxopen y
   minmag=. -~/\@(<./ , >./)
  'minX magX'=. minmag 2{. x
  'minY magY'=. minmag |^:(16=3!:0) xtrmY,,data
  minx + magX * (data - minY) % magY
)

   _5 10 rescale 4 1 9 5 3.5 7;0 10     NB. "out-of-10" to "from _5 to 10"
1 _3.5 8.5 2.5 0.25 5.5
   0 10 rescale 1 _3.5 8.5 2.5 0.25 5.5;_5 10  NB. back to "out-of-10"
4 1 9 5 3.5 7

> From: Marshall Lochbaum
> Sent: Wednesday, 22 September 2010 00:27
> 
> Is there any reason you are using 10 o. instead of | ?
> 
> From: Sherlock, Ric
> Sent: Tuesday, September 21, 2010 5:16 AM
> 
> Thanks guys,
> The following combines the functionality/brevity of versions to date:
> 
> rescale=:3 :0
>   1 rescale y
> :
>   'data xtrm'=. 2{.boxopen y
>   'lowX magX lowY magY'=. (2{.x) ,&(-~/\@(<./,>./)) 10 o. xtrm,,data
>   lowX+magX*(data-lowY)%magY
> )

----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Reply via email to