How to restrict tensorprod (with contraction) to only give certain results?

3 次查看(过去 30 天)
I have vector of matrices A and a vector of vectors M. I want to do matrix multiplication of matrix in A and corresponding vector in M where the result in in R:
A=((1 2 M=((1 R=((5
3 4) 2) 11)
(5 6 (3 (39
7 8)) 4)) 53))
I have three possible solutions:
A=cat(3,[1 2;3 4],[5 6;7 8]) % vector of matrices
M=cat(3,[1;2],[3;4]) % vector of vectors
% 1
N=permute(M,[2 1 3]) % make rows of the vectors
C=A.*N
R1=sum(C,2)
% 2
R2=pagemtimes(A,M)
% 3
R3=tensorprod(A,M,2,1) % contraction acts as matrix multiplication
This gives:
R1(:,:,1) =
5
11
R1(:,:,2) =
39
53
R2(:,:,1) =
5
11
R2(:,:,2) =
39
53
R3(:,:,1,1) =
5 17
11 23
R3(:,:,1,2) =
11 39
25 53
So R1 and R2 I must reshape, but the solution is there. But I have a question about the tensorprod. Because it calculates four vectors. So can I do something with tensorprod so that it gives the upper left and lower right vectors?

采纳的回答

Matt J
Matt J 2024-2-25
No, you cannot. pagemtimes is the appropriate solution.

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Linear Algebra 的更多信息

产品


版本

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by