5

I have a 3d (or in general n-dimensional) matrix A with dimensions

size(A) = [d1 d2 d3 ... dn]. 

Now I want to do a vector multiplication with a column vector v over one of the dimensions (as I would do in 2 dimensions, where I get a vector returned - for instance for d1 = 2, d3 = 4 and size(v) = d2), so that

(A*d)_i = sum(a_ij*v_j). 

Hence I want to reduce by one dimension.

Is there a Matlab function(other than looping) that returns for a d3-dimensional column vector v

(A*v)_ij = sum(A_ijk*v_k). 

I hope this was clear.

Thanks!

4

2 回答 2

3

你可以做得更顺畅一些。对于矩阵reshape只需要 1 个参数,如果没有指定,另一个参数会自动计算出来,这在这种情况下非常有用。

因此,Oli 提出的解决方案可以更简单地写为

A = rand(2,3,4);
v = rand(4,1);

A2 = reshape(A, [], numel(v));      % flatten first two dimensions
B2 = A2*v;
B  = reshape(B2, size(A, 1), []);
于 2012-10-21T16:50:53.743 回答
3

你可以用几个reshape's 做到这一点:

A=rand(2,3,4);
v=rand(1,4);
reshape(reshape(A,[size(A,1)*size(A,2),size(A,3)])*v,[size(A,1) size(A,2)])

基本上,您将 A 重塑为 2D 矩阵 A2((ij),(k))=A((i),(j),(k)):

A2=reshape(A,[size(A,1)*size(A,2),size(A,3)])

然后你做通常的乘法:

对于所有 (ij) B2((ij))=sum_k A2((ij),(k))*v((k)):

B2=A2*v

你重塑回来:

B((i),(j))=B((ij))

B=reshape(B2,[size(A,1) size(A,2)])

我希望这很清楚

于 2012-10-21T16:11:16.283 回答