1
$\begingroup$

I am trying to repair a list of supposedly monotonically and smoothly ascending numbers, like a CDF. An example would be like

aa={1,2,3,4,5,100,200,300,400,500,30,31,32,33,34,35};
bb=Differences[aa]

The objective is to scale up the first five elements to synch up with the sixth which is 100, and then to scale down the first ten elements to bring the first ten elements of the revised list to line up with the remaining six elements.

I have tried the following

MapThread[If[#1>90||#1<-10,MapThread[ReplacePart[aa,#2*Position[bb,#1+1]/Position[bb,#1]]&,{aa[[1;;Position[bb,#2]]]}]&,{bb}]

Apparently, #1 for the first MapThread and #2 for the second MapThread were not executed as intended. Any suggestions or corrections will be much appreciated.

$\endgroup$
2
  • 1
    $\begingroup$ Please fix the syntax error in MapThread... Also, please add the list result you want. There may be an easier way to solve the problem. $\endgroup$
    – creidhne
    Commented Sep 2, 2021 at 0:01
  • $\begingroup$ kglr: Will do. Thanks for the reminder $\endgroup$
    – Z Ming Ma
    Commented Sep 2, 2021 at 3:16

1 Answer 1

2
$\begingroup$

You can use a combination of Split + Fold + Rescale as follows:

split = Split[aa, .5 < #2/# < 3 &]
{{1, 2, 3, 4, 5}, {100, 200, 300, 400, 500}, {30, 31, 32, 33, 34, 35}}
cc = N @ Fold[Join[Rescale[#, {First@#, Last@#}, First /@ {#, #2}], #2] &, split]
{1., 2.43838, 3.87675, 5.31513, 6.75351, 6.75351, 12.5651, 18.3768, 24.1884,
 30., 30., 31., 32., 33., 34., 35.}
ListPlot[{aa, cc}, PlotRange -> All, Joined -> {False, True}]

enter image description here

Use FoldList to see all steps:

N@FoldList[Join[Rescale[#, {First@#, Last@#}, First /@ {#, #2}], #2] &, split] // Column

enter image description here

$\endgroup$
1
  • $\begingroup$ This suggestion is simple and yet works nicely except for its logic or rationale: the use of # and #1 for two subsequent elements of a set instead of two separate sets seems unique. Thanks for the answer. $\endgroup$
    – Z Ming Ma
    Commented Sep 8, 2021 at 16:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.