# How to efficiently do matrix multiplication for 2 specific dimensions of the tensor?

2 visualizzazioni (ultimi 30 giorni)
SC il 20 Nov 2019
Risposto: James Tursa il 20 Nov 2019
Hi,
I have a rank-5 tensor A and a rank-2 matrix B, and I want to captured the first two dimentions of A as slices and do the mutiplication with B to form an output C.
I can do it as below by using 3 for loops. But that is not efficient. How can I do it vectorizedly (i.e. without using the for loops)?
Many thanks!
My current code
A=randn(3,4,8,2,14);
B=randn(7,3);
C=zeros(7,4,8,2,14); % (7, 4) is due to matrix mutiplication of (7, 3)x(3, 4)
for i=1:8
for j=1:2
for k=1:14
A_slice=A(:,:,i,j,k);
C_slice=B*A_slice;
C(:,:,i,j,k)=C_slice;
end
end
end
C % the output I want
##### 0 CommentiMostra -2 commenti meno recentiNascondi -2 commenti meno recenti

Accedi per commentare.

### Risposta accettata

James Tursa il 20 Nov 2019
##### 0 CommentiMostra -2 commenti meno recentiNascondi -2 commenti meno recenti

Accedi per commentare.

### Categorie

Scopri di più su Creating and Concatenating Matrices in Help Center e File Exchange

R2019b

### Community Treasure Hunt

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

Start Hunting!

Translated by