0
$\begingroup$

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.

$\endgroup$
3
  • $\begingroup$ table[[3;;2000,2]]=5 table[[3;;2000,2]]? $\endgroup$
    – kglr
    Commented Apr 18, 2020 at 0:57
  • $\begingroup$ try also MapAt[5 #&, table, {3;;2000,2}] $\endgroup$
    – kglr
    Commented Apr 18, 2020 at 0:59
  • $\begingroup$ 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}} $\endgroup$
    – John
    Commented Apr 18, 2020 at 1:16

2 Answers 2

0
$\begingroup$
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

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

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
$\endgroup$
9
  • $\begingroup$ 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. $\endgroup$
    – John
    Commented Apr 18, 2020 at 1:18
  • $\begingroup$ 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. $\endgroup$
    – dusky
    Commented Apr 18, 2020 at 1:22
  • $\begingroup$ 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. $\endgroup$
    – John
    Commented Apr 18, 2020 at 1:30
  • $\begingroup$ 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}}. $\endgroup$
    – dusky
    Commented Apr 18, 2020 at 1:35
  • $\begingroup$ 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? $\endgroup$
    – John
    Commented Apr 18, 2020 at 1:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.