Using All in MapAt in Mathematica

前端 未结 5 1588
时光取名叫无心
时光取名叫无心 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:16

    Use Part:

    data = {{0, 0.0}, {1, 12.4}, {2, 14.6}, {3, 25.1}}
    
    data[[All, 2]] = Rescale @ data[[All, 2]];
    
    data
    

    Create a copy first if you need to. (data2 = data then data2[[All, 2]] etc.)


    Amending my answer to keep up with ruebenko's, this can be made into a function also:

    partReplace[dat_, func_, spec__] :=
      Module[{a = dat},
        a[[spec]] = func @ a[[spec]];
        a
      ]
    
    partReplace[data, Rescale, All, 2]
    

    This is quite general is design.

提交回复
热议问题