Alex Rivera | Logout

Using All in MapAt in Mathematica

Asked 2011-12-20T18:23:13.260
11

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 all of the second elements without touching the first elements. The neatest way I know is:

Transpose[MapAt[Rescale, Transpose[data], 2]]

There must be a way to do this without so much Transposeing. My wish is for something like this to work:

MapAt[Rescale, data, {All, 2}]

But my understanding is that MapAt takes Position-style specifications instead of Part-style specifications. What's the proper solution?

To clarify,

I'm seeking a solution where I don't have to repeat myself, so lacking double Transpose or double [[All,2]], because I consider repetition a signal I'm not doing something the easiest way. However, if eliminating the repetition requires the introduction of intermediate variables or a named function or other additional complexity, maybe the transpose/untranspose solution is already correct.

Edit
Report

1 Answer

1

This worked for me and a friend

In[128]:= m = {{x, sss, x}, {y, sss, y}}
Out[128]= {{2, sss, 2}, {y, sss, y}}

In[129]:= function[ins1_] := ToUpperCase[ins1];
fatmap[ins2_] := MapAt[function, ins2, 2];

In[131]:= Map[fatmap, m]
Out[131]= {{2, ToUpperCase[sss], 2}, {y, ToUpperCase[sss], y}}
answered 2012-04-05T19:44:58.013

Your Answer