# Multiplying columns with factor [duplicate]

If I have a matrix

matrix=Table[RandomInteger[],{i,3},{j,3}]


And I multiply it with

vector={1,3,5}


All three rows change accordingly.

What do I have to do, when I want to change the columns? Is this:

Transpose@matrix*vector//Transpose


the shortest solution, or do you have better ideas? Because

matrix*Transpose@vector


does not work.

• I think the transpose is a good solution. This also works: vector # & /@ matrix. – Szabolcs Oct 6 '15 at 9:39
• matrix.DiagonalMatrix[vector] – user1066 Jun 7 '17 at 23:40

## 1 Answer

This will multiply the vector by each row of matrix individually:

vector # & /@ matrix


and give the same result as

Transpose[matrix] vector // Transpose


Edit: To get rid of all the useful shorthand to see what is going on underneath, this is identical to what I wrote above:

Map[Function[Times[vector, #]], matrix]

• So "applying a number" onto a number in mathematica is the short version for the function multiplication. vector * # & /@ matrix would be the pure way? – mcocdawc Oct 6 '15 at 9:43
• And thank you very much! – mcocdawc Oct 6 '15 at 9:44
• I like to not use the * for multiplication, it muddles things up. You are applying a function vector # - which multiplies vector by the argument, to the list matrix, whose elements are the rows. – Jason B. Oct 6 '15 at 9:45
• Its just to make things clearer for me. I do not want to make so much use of implicit notation in the beginning. Especially now it is easier for me to understand how to make division #/ vector & /@ matrix. And that the application of a number as a function is defined implicitly as multiplication is just Mathematica, it could also be defined as + or something like that. At least I think so. – mcocdawc Oct 6 '15 at 9:49
• Personally, I would use matrix.DiagonalMatrix[vector] (as noted above). – user1066 Jun 7 '17 at 23:42