Re: normalize a table

• To: mathgroup at smc.vnet.net
• Subject: [mg72817] Re: normalize a table
• From: Jean-Marc Gulliet <jeanmarc.gulliet at gmail.com>
• Date: Sat, 20 Jan 2007 04:24:59 -0500 (EST)
• Organization: The Open University, Milton Keynes, UK
• References: <eopmgk\$rr4\$1@smc.vnet.net>

```Ruth wrote:
> Hi,
>
> I want to do something really simple, which is take a table made of
> pairs and normalize the first number in each pair by dividing by the
> largest.
>
> For instance, I want to transform this list
>
> {{1,0.2},{2,0.3},{3,0.4},{4,0.5}}
>
> into this one
>
> {{1/4, 0.2}, {1/2, 0.3}, {3/4, 0.4}, {1, 0.5}}
>
> The way i did it is through the chain of commands
>
> firsttable={{1,0.2},{2,0.3},{3,0.4},{4,0.5}};
>
>
> top=Max[Table[firsttable[[i,1]],{i,1,Length[firsttable]}]];
>
>
> mytable=Do[newtable=ReplacePart[newtable,newtable[[i,1]]/top,{i,
> 1}],{i,1,Length[firsttable]}]
>
> There must be a more elegant way to do it, but @&# and commands like
> those are still hard to handle for me, and I could not work it out
>
>
> Ruth
>
>

Hi Ruth,

Please, find hereunder three functions that illustrate some possible
approaches to answer your question. What the most elegant code is, I
leave it to you.

In[1]:=
data = {{1, 0.2}, {2, 0.3}, {3, 0.4}, {4, 0.5}};

In[2]:=
Max[data]

Out[2]=
4

In[3]:=
data[[All,1]]/Max[data]

Out[3]=
{1/4, 1/2, 3/4, 1}

In[4]:=
{data[[All,1]]/Max[data], data[[All,2]]}

Out[4]=
{{1/4, 1/2, 3/4, 1}, {0.2, 0.3, 0.4, 0.5}}

In[5]:=
Transpose[{data[[All,1]]/Max[data], data[[All,2]]}]

Out[5]=
{{1/4, 0.2}, {1/2, 0.3}, {3/4, 0.4}, {1, 0.5}}

In[6]:=
myfun1[lst_] := Transpose[{lst[[All,1]]/Max[lst],
lst[[All,2]]}]

In[7]:=
myfun1[data]

Out[7]=
{{1/4, 0.2}, {1/2, 0.3}, {3/4, 0.4}, {1, 0.5}}

In[8]:=
myfun2[lst_] := Module[{maxval = Max[lst]},
({First[#1]/maxval, Last[#1]} & ) /@ lst]

In[9]:=
myfun2[data]

Out[9]=
{{1/4, 0.2}, {1/2, 0.3}, {3/4, 0.4}, {1, 0.5}}

In[10]:=
data /. {x_Integer, (y_)?NumericQ} -> {x/Max[data], y}

Out[10]=
{{1/4, 0.2}, {1/2, 0.3}, {3/4, 0.4}, {1, 0.5}}

In[11]:=
myfun3[lst_] := lst /. {x_Integer, (y_)?NumericQ} ->
{x/Max[lst], y}

In[12]:=
myfun3[data]

Out[12]=
{{1/4, 0.2}, {1/2, 0.3}, {3/4, 0.4}, {1, 0.5}}

Regards,
Jean-Marc

```

• Prev by Date: Re: Dashing and PDF
• Next by Date: Re: Better recursive method?
• Previous by thread: Re: normalize a table
• Next by thread: Creating Dicom Images with 16 Bit format