Using All in MapAt in Mathematica

前端 未结 5 1525
时光取名叫无心
时光取名叫无心 2021-02-20 02:28

I often have a list of pairs, as

data = {{0,0.0},{1,12.4},{2,14.6},{3,25.1}}

and I want to do something, for instance Rescale, to al

5条回答
  •  时光说笑
    2021-02-20 03:28

    How about

    Transpose[{#[[All, 1]], Rescale[#[[All, 2]]]} &@data]
    

    which returns what you want (ie, it does not alter data)

    If no Transpose is allowed,

    Thread[Join[{#[[All, 1]], Rescale[#[[All, 2]]]} &@data]]
    

    works.

    EDIT: As "shortest" is now the goal, best from me so far is:

    data\[LeftDoubleBracket]All, 2\[RightDoubleBracket] = Rescale[data[[All, 2]]]
    

    at 80 characters, which is identical to Mr.Wizard's... So vote for his answer.

提交回复
热议问题