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?
0 个评论
采纳的回答
更多回答(0 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!