# How to divide the second element of a list by a number

I have the following list of number:

{
{0.00008, 342.1}, {0.00016, 221.633}, {0.00024, 134.267},
{0.00032,95.3}, {0.0004,54.4333}, {0.00048,27.1667}, {0.00056,10.8},
{0.00064, 1.03333}, {0.00072,1.1}, {0.0008,1.46667}, {0.00088,2.96667},
{0.00096,1.13333}, {0.00104,0.233333}, {0.00112, 0.}, {0.0012,2.9},
{0.00128,3.4}, {0.00136,1.23333}, {0.00144,1.33333}, {0.00152,2.3},
{0.0016,5.3}, {0.00168,5.53333}, {0.00176,1.83333}, {0.00184,1.63333},
{0.00192,6.36667}, {0.002,6.93333}, {0.00208,2.36667}, {0.00216,2.66667},
{0.00224,5.3}, {0.00232,6.26667}, {0.0024,4.46667}, {0.00248,3.8},
{0.00256,4.5}, {0.00264,5.36667}, {0.00272,4.9}, {0.0028,2.83333},
{0.00288,3.16667}, {0.00296,4.63333}, {0.00304,4.83333}, {0.00312,5.},
{0.0032,5.66667}, {0.00328,5.}, {0.00336,4.13333}, {0.00344,5.13333},
{0.00352,4.6}, {0.0036,4.23333}, {0.00368,5.66667}, {0.00376,5.6},
{0.00384,3.8}, {0.00392,4.33333}, {0.004,5.8}, {0.00408,6.03333},
{0.00416,5.33333}, {0.00424,5.}, {0.00432,4.6}, {0.0044,4.4},
{0.00448,5.43333}, {0.00456,5.23333}, {0.00464,5.36667}, {0.00472,5.63333},
{0.0048,4.33333}, {0.00488,4.33333}, {0.00496,5.76667}, {0.00504,5.8}
}


I'm trying to normalize the second elements by dividing them by the max element (in the specific case 342.1). Can you help me? Thank you.

• max = Max@list[[All, 2]]; list2 = {#[[1]], #[[2]]/max} & /@ list; Alternatively, list2 = (list /. {x_, y_} :> {x, y/max}) Commented May 4, 2022 at 15:50

### Normalize + SubsetMap

list2 = SubsetMap[Normalize[#, Max]&, list, {All, 2}]


{{0.00008, 1.}, {0.00016, 0.64786}, {0.00024, 0.392479}, {0.00032, 0.278574}, {0.0004, 0.159115}, {0.00048, 0.0794116}, {0.00056, 0.0315697}, {0.00064, 0.00302055}, {0.00072, 0.00321543}, {0.0008, 0.00428726}, {0.00088, 0.00867194}, {0.00096, 0.00331286}, {0.00104, 0.000682061}, {0.00112, 0.}, {0.0012, 0.00847705}, {0.00128, 0.00993861}, {0.00136, 0.00360517}, {0.00144, 0.00389749}, {0.00152, 0.00672318}, {0.0016, 0.0154925}, {0.00168, 0.0161746}, {0.00176, 0.00535905}, {0.00184, 0.00477442}, {0.00192, 0.0186106}, {0.002, 0.020267}, {0.00208, 0.00691806}, {0.00216, 0.007795}, {0.00224, 0.0154925}, {0.00232, 0.0183182}, {0.0024, 0.0130566}, {0.00248, 0.0111079}, {0.00256, 0.013154}, {0.00264, 0.0156874}, {0.00272, 0.0143233}, {0.0028, 0.00828217}, {0.00288, 0.00925656}, {0.00296, 0.0135438}, {0.00304, 0.0141284}, {0.00312, 0.0146156}, {0.0032, 0.0165644}, {0.00328, 0.0146156}, {0.00336, 0.0120822}, {0.00344, 0.0150053}, {0.00352, 0.0134464}, {0.0036, 0.0123745}, {0.00368, 0.0165644}, {0.00376, 0.0163695}, {0.00384, 0.0111079}, {0.00392, 0.0126669}, {0.004, 0.0169541}, {0.00408, 0.0176362}, {0.00416, 0.01559}, {0.00424, 0.0146156}, {0.00432, 0.0134464}, {0.0044, 0.0128617}, {0.00448, 0.0158823}, {0.00456, 0.0152977}, {0.00464, 0.0156874}, {0.00472, 0.0164669}, {0.0048, 0.0126669}, {0.00488, 0.0126669}, {0.00496, 0.0168567}, {0.00504, 0.0169541}}

### ApplyTo + Normalize

l3 = list;
l3[[All, 2]] //= Normalize[#, Max] &;

l3 == l2

 True


Alternatively, modify list in place using ApplyTo and Normalize:

list[[All,2]] //= Normalize[#, Max]&;
list == l2

True


With your list stored in data, then

data[[All, 2]] = Normalize[data[[All, 2]], Max];


Here is a visual confirmation of the normalization:

ListPlot[data, PlotRange -> All]


I'll call your list theData. I might do something like this:

With[
{max2nd = Max[theData[[All, 2]]]},
SubsetMap[#/max2nd &, theData, {All, 2}]]


Or this, which might be a little clearer:

With[
{max2nd = Max[theData[[All, 2]]]},
SubsetMap[Rescale[#, {0, max2nd}] &, theData, {All, 2}]]


You could, of course, inline this directly without With:

SubsetMap[Rescale[#, {0, Max[theData[[All, 2]]]}] &, theData, {All, 2}]

lst.{{1,0},{0,1/Max[lst[[All,2]]]}}


check:

(lst.{{1,0},{0,1/Max[lst[[All,2]]]}})==SubsetMap[Normalize[#, Max]&, lst, {All, 2}]

(* True *)


EDIT Thanks to @lericr for pointing out the lack of generality in the original answer.

With data as in the OP:

alist = data[[All, 1]];
blist = Rescale[#, MinMax[#], {Min[#]/Max[#], 1}] &@data[[All, 2]]
ListLinePlot[#, PlotRange -> All, ImageSize -> 300] & /@ {data,
Transpose[{alist, blist}]}


In order to illustrate usage in the presence of bipolar data, consider a random 1-D list called alist:

SeedRandom[11];
alist = RandomReal[{-10, 9}, 20]
MinMax[alist]


{-9.65232, 6.29926}

This will be scaled between 1 and the min value getting divided by the max value of the list.

blist = Rescale[alist, MinMax[alist], {Min[alist]/Max[alist], 1}]


This is a more generic answer compared to the original attempt while using the same Transpose technique.

Original attempt

From the docs:

> Rescale[list]  rescales each element of list to run from 0 to 1 over
> the range Min[list] to Max[list].


Your data (call it data) has two columns:

The first column is: data[[All, 1]]

The second column is: data[[All, 2]]

Rescale 2nd column: Rescale[data[[All, 2]]]

Given these pieces, a Transpose does the job:

res2 = Transpose[{data[[All, 1]], Rescale[data[[All, 2]]]}]


This can be plotted using:

ListPlot[res2[[All, 2]], PlotRange -> All]

• I think this would only work if the min of the second column was 0. So, it does in fact work in this specific case (because the second column min is indeed 0), but it doesn't generalize (unless I've misunderstood your solution). Commented May 4, 2022 at 17:22
• That's right. I should try and submit a more general answer.
– Syed
Commented May 4, 2022 at 17:34
list = {{0.00008, 342.1}, {0.00016, 221.633}, {0.00024, 134.267}, {0.00032, 95.3}};


Using ReplaceAt (new in 13.1)

ReplaceAt[x_ :> x / Max[list], {All, 2}] @ list


{{0.00008, 1.}, {0.00016, 0.64786}, {0.00024, 0.392479}, {0.00032, 0.278574}}

list = {{0.00008, 342.1}, {0.00016, 221.633}, {0.00024, 134.267}, {0.00032, 95.3}};


Grabbing the @eldo's list and using ReplacePart:

ReplacePart[list, {i_, 2} :> list[[i, 2]]/Max[list]]


{{0.00008, 1.}, {0.00016, 0.64786}, {0.00024, 0.392479}, {0.00032, 0.278574}}

I am not sure whether I understand your question correctly. You want to divide every vector by the greater element in it. For example the first vector is 342.1 and second is 221.633. Am I correct? If so, you can try to create a list of "greater value original array" then divide by them respectively (let me assume variable a is your input list):

max=Map[Max[#] &, a];
{{2.3385*10^-7, 1.}, {7.21914*10^-7, 1.}, {1.78748*10^-6,
1.}, {3.35782*10^-6, 1.}, {7.34844*10^-6, 1.}, {0.0000176687,
1.}, {0.0000518519, 1.}, {0.000619357, 1.}, {0.000654545,
1.}, {0.000545453, 1.}, {0.000296629, 1.}, {0.000847061,
1.}, {0.00445715, 1.}, {1., 0.}, {0.000413793, 1.}, {0.000376471,
1.}, {0.00110271, 1.}, {0.00108, 1.}, {0.00066087,
1.}, {0.000301887, 1.}, {0.000303615, 1.}, {0.000960002,
1.}, {0.00112653, 1.}, {0.000301571, 1.}, {0.000288462,
1.}, {0.000878872, 1.}, {0.000809999, 1.}, {0.000422642,
1.}, {0.000370213, 1.}, {0.000537313, 1.}, {0.000652632,
1.}, {0.000568889, 1.}, {0.000491925, 1.}, {0.000555102,
1.}, {0.000988236, 1.}, {0.000909473, 1.}, {0.000638849,
1.}, {0.000628966, 1.}, {0.000624, 1.}, {0.000564706,
1.}, {0.000656, 1.}, {0.000812904, 1.}, {0.00067013,
1.}, {0.000765217, 1.}, {0.000850394, 1.}, {0.000649411,
1.}, {0.000671429, 1.}, {0.00101053, 1.}, {0.000904616,
1.}, {0.000689655, 1.}, {0.000676243, 1.}, {0.00078, 1.}, {0.000848,
1.}, {0.00093913, 1.}, {0.001, 1.}, {0.00082454, 1.}, {0.000871338,
1.}, {0.000864596, 1.}, {0.00083787, 1.}, {0.00110769,
1.}, {0.00112615, 1.}, {0.000860115, 1.}, {0.000868966, 1.}}

$$$$
`