Multiply then sum elements of two matrices

10 visualizaciones (últimos 30 días)
F
F el 28 de Feb. de 2011
Comentada: Ron Fredericks el 12 de Dic. de 2020
What's the best way to 1) multiply all the elements between two similar sized matrices then 2) sum them into one single number/outcome?
  1 comentario
Ron Fredericks
Ron Fredericks el 12 de Dic. de 2020
Another way to calculate Frobenious dot product:
trace( A'*B );

Iniciar sesión para comentar.

Respuesta aceptada

Walter Roberson
Walter Roberson el 28 de Feb. de 2011
It is not clear from your question whether corresponding elements are to be multiplied or if you are wanting to do a matrix multiplication.
If corresponding elements are to be multiplied, then you could calculate the sum-of-products using
dot(A(:),B(:))
  1 comentario
Edoardo Bezzeccheri
Edoardo Bezzeccheri el 25 de Mayo de 2016
The mathematical name of the operation is called Frobenius Product, by the way.

Iniciar sesión para comentar.

Más respuestas (3)

Bruno Luong
Bruno Luong el 28 de Feb. de 2011
I believe
sum(A(:).*B(:))
would be faster than dot(...) if that matter.
  2 comentarios
Walter Roberson
Walter Roberson el 28 de Feb. de 2011
Heh. I just looked at the source for dot. It does sum(conj(a).*b) so yes, your suggestion should be slightly faster due to have a small bit less overhead.
F
F el 1 de Mzo. de 2011
Hi Bruno and Walter, thanks for the answers. I used them both and they worked! I'm new to Matlab so I don't know yet how to clock the processing time....I will learn. Great user community feedback!

Iniciar sesión para comentar.


James Tursa
James Tursa el 28 de Feb. de 2011
  2 comentarios
James Tursa
James Tursa el 28 de Feb. de 2011
Or if you prefer to stay with MATLAB built-in functions:
A(:).'*B(:)
F
F el 1 de Mzo. de 2011
James, thanks for the answer as well!

Iniciar sesión para comentar.


Matt Fig
Matt Fig el 28 de Feb. de 2011
Another (this should be faster than calling SUM):
reshape(A,1,[])*reshape(B,[],1)
  9 comentarios
James Tursa
James Tursa el 28 de Feb. de 2011
e.g., 32-bit WinXP Intel Core 2 Duo:
>> A = rand(3000) + rand(3000)*1i;
>> B = rand(3000) + rand(3000)*1i;
>>
>> tic;dot(A(:),B(:));toc
Elapsed time is 0.158458 seconds.
>> tic;dot(A(:),B(:));toc
Elapsed time is 0.165634 seconds.
>>
>> tic;A(:)'*B(:);toc
Elapsed time is 0.101896 seconds.
>> tic;A(:)'*B(:);toc
Elapsed time is 0.101962 seconds.
>>
>> mtimesx('speedomp')
ans =
SPEEDOMP
>> tic;mtimesx(A(:),'C',B(:));toc
Elapsed time is 0.061016 seconds.
>> tic;mtimesx(A(:),'C',B(:));toc
Elapsed time is 0.061558 seconds.
The symmetric case difference is even more dramatic:
>> tic;dot(A(:),A(:));toc
Elapsed time is 0.146473 seconds.
>> tic;dot(A(:),A(:));toc
Elapsed time is 0.146098 seconds.
>>
>> tic;A(:)'*A(:);toc
Elapsed time is 0.084071 seconds.
>> tic;A(:)'*A(:);toc
Elapsed time is 0.082746 seconds.
>>
>> tic;mtimesx(A(:),'C',A(:));toc
Elapsed time is 0.032382 seconds.
>> tic;mtimesx(A(:),'C',A(:));toc
Elapsed time is 0.032521 seconds.
F
F el 1 de Mzo. de 2011
Matt, thanks for your feedback as well. Yours works too - but I never would have figured that out...something to think about. Again, great user community here.

Iniciar sesión para comentar.

Categorías

Más información sobre Operators and Elementary Operations en Help Center y File Exchange.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by