bottaio
bottaio

Reputation: 5073

Multiply horizontal times vertical vectors of matrices and sum the result without loop

Basically, I have 2 matrices and I want to rewrite this:

for i = 1:m
  result += A(:, i) * B(:, i)';
end

Without using for loop and I have no idea how to approach that. I've been strugling to solve it for several hours so I ask you guys to help me out.

Upvotes: 0

Views: 237

Answers (1)

Suever
Suever

Reputation: 65430

This is no different than simply doing matrix multiplication of A and the transpose of B.

result = A * B.';

Just for completeness...

m = 10;
A = rand(15, m);
B = rand(12, m);

result = zeros(size(A, 1), size(B, 1));
for k = 1:m
    result = result + A(:,k) * B(:,k).';
end

difference = max(max(abs(result - A *B.')));

difference = 

    8.8818e-16

Upvotes: 3

Related Questions