# How can I divide the elements in the 2nd column of a table by given number? [duplicate]

Suppose I build a table of pairs like so.

table = Table[{x, f[x]}, {x, a, b, c}]


After getting table, I want to derive table1 from it, by dividing for the 2nd element in each pair by 25, so that table1 is equivalent to

Table[{x, f[x]/25}, {x, a, b, c}]


How can I do this?

• Try MapAt[#/25 &, table, {All, 2}] or table.DiagonalMatrix[{1, 1/25}]. Commented Aug 11, 2017 at 11:46
• Also, {#1, #2/25} & @@@ table Commented Aug 11, 2017 at 13:46
• data2 = data /. {x_, y_} -> {x, y/2} Commented Aug 11, 2017 at 13:47

J.M.

 MapAt[#/25 &, table, {All, 2}]
table.DiagonalMatrix[{1, 1/25}]


m_goldberg

{#1, #2/25} & @@@ table


Alucard

table /. {x_, y_} -> {x, y/25}


Timing analysis:
Update it turned out the previous barchart was wrong

  table = RandomReal[{0, 1000}, {10^7, 2}];
fun1[x_List] := MapAt[#/25 &, x, {All, 2}]
fun2[x_List] := x.DiagonalMatrix[{1, 1/25}]
fun3[x_List] := {#1.#2/25} & @@@ table
fun4[x_List] := Module[{table1 = x}, table1[[All, 2]] /= 25.; table1]
fun[x_List] := x /. {p_, y_} -> {p, y/25}
BarChart[{RepeatedTiming[fun1[data]][[1]],
RepeatedTiming[fun2[table]][[1]], RepeatedTiming[fun3[table]][[1]],
RepeatedTiming[fun[table]][[1]], RepeatedTiming[fun4[table]][[1]] },
ChartLabels -> {"J.M. 1", "J.M.2", "m_goldberg", "Alucard",
"Schumacher"}]


• i made a simple barchart with the absolute timings, just to see which solution was faster. the results says that the faster solution is the second one posted by J.M. ( table.DiagonalMatrix[{1, 1/25}]), while the third one is the slowest, the other 2 are equal Commented Aug 11, 2017 at 14:02
• @Alucard, since this post is CW, please edit this to include your BarChart[] and any code you used for testing. You might also consider using RepeatedTiming[]. Commented Aug 11, 2017 at 14:03
• @J.M. what does it mean CW? Commented Aug 11, 2017 at 14:04
• For matrices with many columns, the following should be even more efficient than the solution by @J.M.: fun4[x_List] := Module[{table1 = x}, table1[[All, 2]] /= 25.; table1] Commented Aug 11, 2017 at 18:04
• In addition, Inner variations on J.M's solution, funx[x_List] := Inner[Times, x, DiagonalMatrix[{1, 1/25}]] and funy[x_List] := Inner[Times, x, {1, 1/25}, List] are very significantly slower. (Both at least 50x slower on my machine). Dot appears to be very efficient. Commented Aug 12, 2017 at 11:55

Somethings is wrong with the timings in the other answer. Here is what BenchmarkPlot in the GeneralUtilites package gives.

fun1[x_List] := MapAt[#/25 &, x, {All, 2}]
fun2[x_List] := x.DiagonalMatrix[{1, 1/25}]
fun3[x_List] := {#1, #2/25} & @@@ x
fun4[x_List] := Module[{table1 = x}, table1[[All, 2]] /= 25.; table1]
fun[x_List] := x /. {p_, y_} :> {p, y/25}

Needs["GeneralUtilities"]
BenchmarkPlot[{fun, fun1, fun2, fun3, fun4}, RandomReal[9, {#, 2}] &]


• ah now i see the error, when i added schumacher's method i changed the name of the list from data to table1 and i forgot to update the rest of the code! Commented Aug 12, 2017 at 5:54
• That fun3`... that should be a comma and not a dot. Commented Aug 12, 2017 at 6:07