# Multiply an array by an especific number at an specific position in the array

If I have a table of let's say 2000 values in column 1 and 2, how can I multiply only an specific part of that table by a certain number?. In particular, I want to multiply the second column only from row 3 to row 2000 (not including row 1 and 2) by a number (let's say 5) and leaving column 1 intact. I would very much appreciate your help.

I am aware of Elegant operations on matrix rows and columns but I could not find a solution for this in particular there.

• table[[3;;2000,2]]=5 table[[3;;2000,2]]?
– kglr
Apr 18, 2020 at 0:57
• try also MapAt[5 #&, table, {3;;2000,2}]
– kglr
Apr 18, 2020 at 0:59
• Thank you kglr. The first method works but I cannot access column 1. I would like to have column 2 multiplied by the number while at the same time column 1 does not dissapear. The second method does not work: If I have table = Table[{1, 2}, 10]; and then I use MapAt[5 # &, table, {3 ;; 10, 2}], then I get {{1, 2}, {1, 2}, {78125, 3906250}, {78125, 3906250}, {78125, 3906250}, {78125, 3906250}, {78125, 3906250}, {78125, 3906250}, {78125, 3906250}, {78125, 3906250}}
– John
Apr 18, 2020 at 1:16

table = Table[{1, 2}, 10];


To create new tables without changing table:

table2 = Module[{t = #}, t[[2 ;; 10, 2]] = 5 t[[2 ;; 10, 2]]; t] & @ table;
table3 = Module[{t = #}, t = MapAt[5 # &, t, {2 ;; 10, 2}]] & @ table;
table4 = Module[{t = #}, t[[2 ;; 10, 2]] *= 5; t] & @ table;

{MatrixForm /@ {table, table2, table3, table4},
{"table", "table2", "table3", "table4"}}], Spacer[10]]


• Thank you very much kglr! This works perfect!
– John
Apr 18, 2020 at 1:56

Multiplying the row range $$a_1$$ to $$a_2$$ and column range $$b_1$$ to $$b_2$$ by the scalar $$c$$ in the matrix $$A$$:

A[[a1;;a2, b1;;b2]] *= c

• Unfortunately, this method does not seem to work. It gives a multiplication by 5, every single time you hit shift + Enter. In other words, at first it multiplies by 5 and then you do it again and now is the last by 5 and then the last by 5 again and so on, saving the last calculation.
– John
Apr 18, 2020 at 1:18
• That's what any line of code that depends on the previous state of $A$ will do. If you want a line of code that fixes $A$ no matter what, you will have to hardcode the value into the line A[[a1;;a2, b1;;b2]] = c * hard_coded_A[[a1;;a2, b1;;b2]]. This is bad practice. If you write your notebook sequentially assuming that line will be hit only once, it will work. It will be more readable that way too. Apr 18, 2020 at 1:22
• I understand but the code does not seem to work very well either in any case. For instance, if I have table = Table[{1, 2}, 10]; and then I use your code as table[[3 ;; 10]] *= 5, in the very first iteration it will give {{5, 50}, {5, 50}, {5, 50}, {5, 50}, {5, 50}, {5, 50}, {5, 50}, {5, 50}}... which is obviously wrong, as it seems to multiply by 5 the first row and by 25 the second.
– John
Apr 18, 2020 at 1:30
• I don't follow. The following code m = Table[{1,2},10]; m[[3;;10]]*=5; outputs {{5, 10}, {5, 10}, {5, 10}, {5, 10}, {5, 10}, {5, 10}, {5, 10}, {5, 10}}. Apr 18, 2020 at 1:35
• Correct for the first iteration. But remember that I would like to have the first row unchanged and in that code both rows are multiplied. Is there any way to have only the second row multiplied by 5 and not both rows?
– John
Apr 18, 2020 at 1:39