Azzera filtri
Azzera filtri

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

3 visualizzazioni (ultimi 30 giorni)
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?

Risposta accettata

Matt J
Matt J il 25 Feb 2024
No, you cannot. pagemtimes is the appropriate solution.

Più risposte (0)

Categorie

Scopri di più su Linear Algebra in Help Center e File Exchange

Prodotti


Release

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by