0

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.

J. M.'s missing motivation
  • 124,525
  • 11
  • 401
  • 574
John
  • 471
  • 2
  • 8
  • table[[3;;2000,2]]=5 table[[3;;2000,2]]? – kglr Apr 18 '20 at 00:57
  • try also MapAt[5 #&, table, {3;;2000,2}] – kglr Apr 18 '20 at 00: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 '20 at 01:16

2 Answers2

1

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
dusky
  • 401
  • 3
  • 9
  • 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 '20 at 01: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. – dusky Apr 18 '20 at 01: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 '20 at 01: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}}. – dusky Apr 18 '20 at 01: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 '20 at 01:39
  • Then what you're looking for is m = Table[{1,2},10]; m[[2, 3;;10]]*=5; i.e. you forgot to specify the second column. – dusky Apr 18 '20 at 01:40
  • Thank you very much dskeletov. However, even though the code works now specifying the second column, it removes the first column. In other words, I would like the second column multiplied by 5 while at the same time the first column does not dissapear, so that I can have say Table[{1, 10}, 10]; if my initial table was m=Table[{1, 2}, 10]; – John Apr 18 '20 at 01:46
  • Also please remember from the question that I would like not only to still have column 1 intact but also not to loose the two values (row 1 and 2) from column 2 which are not multiplied by 5 – John Apr 18 '20 at 01:53
  • Ah, I got it backwards above, it should've been m = Table[{1,2},10]; m[[3;;10, 2]]*=5;. Anyway, glad you got it working! – dusky Apr 18 '20 at 01:59
0
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;

Row[MapThread[Labeled[##, Top] &,
   {MatrixForm /@ {table, table2, table3, table4}, 
   {"table", "table2", "table3", "table4"}}], Spacer[10]]

enter image description here

kglr
  • 394,356
  • 18
  • 477
  • 896