Arjay7891
Arjay7891

Reputation: 11

Matlab multiply rows of matrices - vectorized or bsxfun

I have 2 5000x10 matrices and I would like to get a length-5000 vector that has as entries the vector products of the corresponding rows in the two matrices. I.e. the first entry should be row1 of matrix1 * row1 of matrix2 transposed and so on.

I can implement this with a for loop:

result = zeros(5000)
for i = 1:5000
  result(i) = matrix1(i,:)*matrix2(i,:)'
end

but is there a way to do this vectorized or with bsxfun?

thanks!

Upvotes: 0

Views: 52

Answers (1)

beaker
beaker

Reputation: 16791

You can just use element-wise multiplication and sum the rows:

result = sum(matrix1.*matrix2, 2);

Upvotes: 1

Related Questions