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.
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).'
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 .
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With