Using the Dot
product:
tab.DiagonalMatrix[{1,2,1,1,2,1}]
Or, if there are a lot of columns:
Edit
Lucas Lang suggested a neat modification, such as the following, to the original SparseArray
answer (and thanks!):
tab.DiagonalMatrix[SparseArray[Thread[{2,5}->2], {6},1]]
Original answer
tab.SparseArray[{{2,2}->2, {5,5}-> 2,Band[{1, 1}] -> 1}, {6,6}]
With Inner
:
Inner[Times, tab, {1,2,1,1,2,1}, List]
In addition, if the table consists of only two columns (a table of {x,y} values, maybe) and if it is desired to multiply all y-values by 2:
table.{{1,0},{0,2}}
For example:
tab[[All,;;2]].{{1,0},{0,2}}
Modify in place using ApplyTo (//=)
(modify tab
, not a copy)
tab[[All,{2,5}]]//= 2#&
or using the new function syntax:
tab[[;;,{2,5}]]//=(x |-> 2 x)
Edit 2: ReplaceAt
Since v13.1,ReplaceAt
may be used:
result = ReplaceAt[tab,x_ :> 2 x, {All,{2,5}}];
result == tab.DiagonalMatrix[{1,2,1,1,2,1}]
(* True *)
Edit 3: Threaded
As kglr has taught us (see here and here), an even more convenient method is arguably with Threaded
want = tab.DiagonalMatrix[{1,2,1,1,2,1}];
tab Threaded[{1,2,1,1,2,1}] == want
tab Threaded[SparseArray[Thread[{2,5}->2],6,1]] == want
(* True *)
(* True *)
A further example:
myTable = Array[Subscript[a, Row[{##}]] &, {3, 6}];
myTable//TeXForm
$$
\left(
\begin{array}{cccccc}
a_{11} & a_{12} & a_{13} & a_{14} & a_{15} & a_{16} \\
a_{21} & a_{22} & a_{23} & a_{24} & a_{25} & a_{26} \\
a_{31} & a_{32} & a_{33} & a_{34} & a_{35} & a_{36} \\
\end{array}
\right)
$$
myTable Threaded[SparseArray[Thread[{2,5}->2],Dimensions@myTable[[2]],1]]
$$
\left(
\begin{array}{cccccc}
a_{11} & 2 a_{12} & a_{13} & a_{14} & 2 a_{15} & a_{16} \\
a_{21} & 2 a_{22} & a_{23} & a_{24} & 2 a_{25} & a_{26} \\
a_{31} & 2 a_{32} & a_{33} & a_{34} & 2 a_{35} & a_{36} \\
\end{array}
\right)
$$
myTable Threaded[SparseArray[{2->2,3->10,6->100},Dimensions@myTable[[2]],1]]
$$
\left(
\begin{array}{cccccc}
a_{11} & 2 a_{12} & 10 a_{13} & a_{14} & a_{15} & 100 a_{16} \\
a_{21} & 2 a_{22} & 10 a_{23} & a_{24} & a_{25} & 100 a_{26} \\
a_{31} & 2 a_{32} & 10 a_{33} & a_{34} & a_{35} & 100 a_{36} \\
\end{array}
\right)
$$