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.
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]
Comments
Post a Comment