1

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.

mcocdawc
  • 451
  • 3
  • 13

1 Answers1

3

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]
Jason B.
  • 68,381
  • 3
  • 139
  • 286
  • 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 06 '15 at 09:43
  • And thank you very much! – mcocdawc Oct 06 '15 at 09: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 06 '15 at 09: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 06 '15 at 09:49
  • Personally, I would use matrix.DiagonalMatrix[vector] (as noted above). – user1066 Jun 07 '17 at 23:42