0

我有一组标量和矩阵:

w1, w2...wn
A1, A2... An

如何获得

w1*A1 + w2*A2 + ... + wn*An

没有循环?以及如何有效地获得

w1*(b1*c1) + w2*(b2*c2) + ... + wn*(bn*cn)

向量在哪里bi和是矩阵,而不是标量?cibi*ci

4

2 回答 2

0

编辑:我有一个更好的解决方案。

如果您的矩阵 An 存储在大小为 3-D 的矩阵中,P x Q x N并且您的权重存储在大小的权重向量中,则以下命令执行加权平均:An = A(:,:,n)n = 1, 2, ..., Nw1 x N

B = reshape(w*reshape(permute(A,[3,1,2]),N,[]),[P,Q]);
于 2017-02-04T19:11:05.797 回答
0

第一个问题可以通过bsxfun以下方式解决:

% create matrices
a=[1 2]
b=randi(9,[3 3 2])

% now you will have to reshape a, so that its non-singleton dimensions match b
% i.e. a is 1 x 2 and b is 3 x 3 x 2, so second dimension of (=2) should match 
% 3rd dimension of b (=2). Thus a should be of shape `1 x 1 x 2`. Then you can 
% multiply using bsxfun and sum along 3rd dimension as follows

sum(bsxfun(@times, permute(a,[3 1 2]), b),3)
于 2017-02-04T19:29:19.443 回答