6
votes

I have two matrices:

A = [1 2; 
     3 4; 
     5 6] 

B = A'

The multiplication should take in the way as if row and column vector is extracted from both.
C = B(:,i) * A(i,:) such that for first instance (1st row and 1st column) the result would be:

[1 2; 
 2 4]

This will be summed up vertically to obtain [3 6]. This sum will give final answer 9. Likewise, 2nd row & 2nd column, 3rd row & 3rd column and so on if matrix size is higher.

This final scalar value will be used for comparing which row and its corresponding column has high yield.

3
The question is edited. The guess of final answer is right [9; 49; 121]user9003011

3 Answers

8
votes

Your required result is actually mathematically equivalent of:

sum(A,2).^2   %or  sum(A,2) .* sum(A,2) 

If A and B are not transpose of each other then:

sum(A,2).* sum(B,1).' 
3
votes

You can use sum:

result = sum(bsxfun(@times,sum(A,2), B.'),2);

Or in the recent version of MATLAB you can write:

result = sum(sum(A,2).*B.',2)

Previous answer:

You can use permute:

result = sum(reshape(permute(A,[2 3 1]) .* permute(A,[3 2 1]),[],size(A,1)));

Or in the case of A and B:

result = sum(reshape(permute(B,[1 3 2]) .* permute(A,[3 2 1]),[],size(A,1)));

result = [9 49 121]

Thanks to @TommasoBelluzzo and @SardarUsama .

2
votes

If your Matrix is of Size Nx2, then one possible answer is

A.*A * [1;1] + 2*A(:,1).*A(:,2)