1
$\begingroup$

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.

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

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]
$\endgroup$
  • $\begingroup$ So "applying a number" onto a number in mathematica is the short version for the function multiplication. vector * # & /@ matrix would be the pure way? $\endgroup$ – mcocdawc Oct 6 '15 at 9:43
  • $\begingroup$ And thank you very much! $\endgroup$ – mcocdawc Oct 6 '15 at 9:44
  • $\begingroup$ 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. $\endgroup$ – Jason B. Oct 6 '15 at 9:45
  • $\begingroup$ 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. $\endgroup$ – mcocdawc Oct 6 '15 at 9:49
  • $\begingroup$ Personally, I would use matrix.DiagonalMatrix[vector] (as noted above). $\endgroup$ – user1066 Jun 7 '17 at 23:42

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