How to encode this matrix multiplication?

I have two matrices:

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

B = A'

Multiplication should be performed as if rows and columns were extracted from both. C = B(:,i) * A(i,:)that for the first instance (1st row and 1st column) the result will be:

[1 2; 
 2 4]

This will be summarized vertically to get [3 6]. This amount will give a final answer 9. Similarly, the 2nd row and 2nd column, 3rd row and 3rd column, and so on, if the matrix size is higher.

This final scalar value will be used to compare which row and corresponding column have a high yield.

+6
source share
3 answers

sum:

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

MATLAB :

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

:

permute:

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

A B:

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

result = [9 49 121]

@TommasoBelluzzo @SardarUsama.

+3

:

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

A B , :

sum(A,2).* sum(B,1).' 
+7

Nx2, -

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

All Articles