Using All in MapAt in Mathematica

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

    Here is another approach:

    op[data_List, fun_] := 
     Join[data[[All, {1}]], fun[data[[All, {2}]]], 2]
    
    op[data, Rescale]
    

    Edit 1:

    An extension from Mr.Wizard, that does not copy it's data.

    SetAttributes[partReplace, HoldFirst]
    partReplace[dat_, func_, spec__] := dat[[spec]] = func[dat[[spec]]];
    

    used like this

    partReplace[data, Rescale, All, 2]
    

    Edit 2: Or like this

    ReplacePart[data, {All, 2} -> Rescale[data[[All, 2]]]]
    

提交回复
热议问题