필터 지우기
필터 지우기

Multiplying certain dimensions of two 3D matrices

조회 수: 5 (최근 30일)
Michael
Michael 2012년 5월 14일
편집: Comrun Yousefzadeh 2020년 6월 17일
Hi
I've got two matrices, one 30x30x50 and one 30x30x80
I would like to multiply them in such a way that the result is a 50x80 matrix ie. an implicit sum over all of the 30x30 common elements.
eg. A*B = C
C(k,l) = A(i,j,k)*B(i,j,l) summed over all i, j
Is there a simple way to write this? I would rather avoid complications using "reshape".
Thanks, Mike

채택된 답변

Andrei Bobrov
Andrei Bobrov 2012년 5월 14일
Idea of Sean de Wolski
C = squeeze(sum(sum(bsxfun(@times,a,permute(b,[1 2 4 3])))))
or
s = size(a)
C = squeeze(sum(bsxfun(@times,reshape(a,s(1)*s(2),[])',reshape(b,1,s(1)*s(2),[])),2))
  댓글 수: 1
Andrei Bobrov
Andrei Bobrov 2012년 5월 14일
Hi Michael! Thank you for "accpted", but Teja's variant is better than my.

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

Teja Muppirala
Teja Muppirala 2012년 5월 14일
A = rand(30,30,50);
B = rand(30,30,80);
C = reshape(A,900,[])'*reshape(B,900,[]);
  댓글 수: 4
Michael
Michael 2012년 5월 14일
I'll use this one, thanks everyone
Michael
Michael 2012년 5월 14일
I have one more related question,
I'm also trying to compute D = C*A, I think A must be transposed for this to make sense: the (i,j,l) element of D is = (k,l) of C times (i,j,k) of A summed over k. I've experimented a bit and found this,
reshape(reshape(A,900,[])*C,30,30,[]);
Is it the best method? I haven't found a general rule to do these automatically yet and I'm not sure if the 'outer' reshape is preserving the form of my 30x30 matrix and not inadvertently flipping it about a diagonal.
Thanks very much for any help

댓글을 달려면 로그인하십시오.


Comrun Yousefzadeh
Comrun Yousefzadeh 2020년 6월 17일
편집: Comrun Yousefzadeh 2020년 6월 17일
I have a related questoin. I'm trying to multiply a two dimentioanl array to a specific dimension of a 3-D matrix. For example, A=2000*2000*72, B=72*3 and I'm trying to reach C=2000*2000*3. In other words B should be multiplied to the 3rd dimension of A and repeated (element wise) for 2000*2000 points in the first two dimensions. Currently I'm using a loop like this:
for ii=1:size(A,1)
for jj=1:size(A,2)
A1=A(ii,jj,:);
A1=A1(:);
C1=B'*A1; % C1 has dimension of 3*1
C2=D*C1; %D is a 3-3 matrix therefore, C2 has the same dimension as C1
C(ii,jj,:)=C2(:,1);
end
end
What is the better way to avoid the loop?
Thanks,

카테고리

Help CenterFile Exchange에서 Resizing and Reshaping Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by