Daniel Paczuski Bak
Daniel Paczuski Bak

Reputation: 4088

Multiply each column in matrix by column in vector

sampleInput =

    2.1053   -4.8512    4.6223    0.9665    1.0000


hiddenWeights =

   -0.6342   -0.2089    0.4533   -0.6182   -0.3663
   -0.9465   -1.0770   -0.2668    0.7077   -1.1656
    0.0936   -0.2853   -0.1408    0.6193   -0.5481
    1.4253    0.3770   -0.6710    0.1069    0.0310

I want the result to be hiddenWeights, with each column being equal to the previous column * 2.1053. So the first column of hiddenWeights would be:

2.1053 * -0.6342
2.1053 * -0.9464
etc.

Upvotes: 0

Views: 50

Answers (2)

Luis Mendo
Luis Mendo

Reputation: 112759

Another possibility is to transform sampleInput into a diagonal matrix and apply matrix multiplication:

result = hiddenWeights*diag(sampleInput);

Upvotes: 1

rayryeng
rayryeng

Reputation: 104555

Sounds like a job for bsxfun:

out = bsxfun(@times, hiddenWeights, sampleInput);

Here, sampleInput will duplicate its row by as many times as there are rows in hiddenWeights and will undergo an element-wise multiplication with that new matrix with hiddenWeights. The result will be each column of hiddenWeights will multiply with the corresponding column in sampleInput, and will be what you desire.

Upvotes: 7

Related Questions