2D matrix multiply with 3D array

1 Ansicht (letzte 30 Tage)
Qiu Xu
Qiu Xu am 23 Okt. 2021
Kommentiert: Qiu Xu am 24 Okt. 2021
Hi,
I have two 2D matrix u and v, and a 3D array B. I want to perform the following calculations, for example
u=rand(200,300);
v=rand(200,300);
B=rand(60,60,300)
c=0;
for j=1:300
u1=0;
v1=0;
for k=1:200
u1=u1+u(k,j)*B(:,:,k);
v1=v1+v(k,j)*B(:,:,k);
end
c=c+u1.*v1;
end
Due to two for loops, it is very slowly for the larger matrix u, v and B. Are there some good methods to compute c without using any for loop?.
Thanks a lot.
  1 Kommentar
Matt J
Matt J am 23 Okt. 2021
Are you sure the size of B shouldn't be 60x60x200? If not, your code example ignores the final 100 pages of B.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Matt J
Matt J am 23 Okt. 2021
Bearbeitet: Matt J am 23 Okt. 2021
[m,n,p]=size(B);
Br=reshape(B,[],p);
Br=Br(:,1:200); %might not be necessary
U1=Br*u;
V1=Br*v;
c=reshape( sum(U1.*V1,2) , m,n);
  1 Kommentar
Qiu Xu
Qiu Xu am 24 Okt. 2021
Thanks, I have make a mistake that the size of B should be 60x60x200. Your method is very good.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Multidimensional Arrays finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by