Tensor contraction in Matlab [duplicate]

2020-07-14 05:29发布

Possible Duplicate:
MATLAB: How to vector-multiply two arrays of matrices?

Is there a way to contract higher-dimensional tensors in Matlab?

For example, suppose I have two 3-dimensional arrays, with these sizes:

size(A) == [M,N,P]
size(B) == [N,Q,P]

I want to contract A and B on the second and first indices, respectively. In other words, I want to consider A to be an array of matrices of size [M,N] and B to be equal length array of [N,Q] matrices; I want to multiply these arrays element-by-element (matrix-by-matrix) to get something of size [M,Q,P].

I can do this via a for-loop:

assert(size(A,2) == size(B,1));
assert(size(A,3) == size(B,3));

M = size(A,1);
P = size(A,3);
Q = size(B,2);

C = zeros(M, Q, P);
for ii = 1:size(A,3)
    C(:,:,ii) = A(:,:,ii) * B(:,:,ii);
end

Is there a way to do this that avoids the for-loop? (And perhaps works with arrays of an arbitrary number of dimensions?)

1条回答
Melony?
2楼-- · 2020-07-14 05:57

Here is a solution (similar to what was done here) that computes the result in a single matrix-multiplication operation, although it involves heavy manipulation of the matrices to put them into desired shape. I then compare it to the simple for-loop computation (which I admit is a lot more readable)

%# 3D matrices
A = rand(4,2,3);
B = rand(2,5,3);
[m n p] = size(A);
[n q p] = size(B);

%# single matrix-multiplication operation (computes more products than needed)
AA = reshape(permute(A,[2 1 3]), [n m*p])';      %'# cat(1,A(:,:,1),...,A(:,:,p))
BB = reshape(B, [n q*p]);                         %# cat(2,B(:,:,1),...,B(:,:,p))
CC = AA * BB;
[mp qp] = size(CC);

%# only keep "blocks" on the diagonal
yy = repmat(1:qp, [m 1]);
xx = bsxfun(@plus, repmat(1:m,[1 q])', 0:m:mp-1); %'
idx = sub2ind(size(CC), xx(:), yy(:));
CC = reshape(CC(idx), [m q p]);

%# compare against FOR-LOOP solution
C = zeros(m,q,p);
for i=1:p
    C(:,:,i) = A(:,:,i) * B(:,:,i);
end
isequal(C,CC)

Note that the above is performing more multiplications than needed, but sometimes "Anyone who adds, detracts (from execution time)". Sadly this is not the case, as the FOR-loop is much faster here :)

My point was to show that vectorization is not easy, and that loop-based solutions are not always bad...

查看更多
登录 后发表回答