Dot product of tensor

8 次查看(过去 30 天)
Hi, I have two tensors A and B, of sizes (ma, na, 3), and (mb, nb, 3).
I would like to create C of size(ma, na, mb, nb) with C(i,j,k,h) = dot(A(i,j,:),B(k,h,:));
What is the fastest and most memory efficient way of achieving this? Many thanks!

采纳的回答

Bruno Luong
Bruno Luong 2022-4-6
编辑:Bruno Luong 2022-4-6
If you have the R2022a, a single statement will do
A=rand(2,3,3);
B=rand(4,5,3);
C=tensorprod(A,B,3);
size(C)
ans = 1×4
2 3 4 5
  3 个评论
Bruno Luong
Bruno Luong 2022-4-6
I don't know what you meant by "sums" but MATLAB auto-expansion and reshape is like tensor operators
A=rand(2,3,3);
B=rand(4,5,3);
C = sum(A,3) + reshape(sum(B,3),[1 1 size(B,1) size(B,2)])
% or this?
C = reshape(A, [size(A,1) size(A,2) 1 1 size(A,3)]) + ...
reshape(B, [1 1 size(B)])
Leonardo Mutti
Leonardo Mutti 2022-4-6
Okay perfect, the second works well, many thanks.

请先登录,再进行评论。

更多回答(1 个)

Matt J
Matt J 2022-4-6
编辑:Matt J 2022-4-6
Ar=reshape(A,[],3);
Br=reshape(B,[],3);
C=reshape( Ar*Br.' ,ma,na,mb,nb);

类别

Help CenterFile Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息

产品


版本

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by