0

我有标量的团体和respertively矩阵:如何获得没有循环的加权矩阵的平均值?

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

如何获得

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

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

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

bici是矢量,但bi*ci是矩阵,不是标?

+0

您是否尝试过只需要输入你写的东西(如'W1 * A1 + W2 * A2 + ...... + WN * An')INT Matlab?它是基本的Matlab矩阵运算:[times](https://se.mathworks.com/help/matlab/ref/mtimes.html)和[plus](https://se.mathworks.com/help/matlab/ref /plus.html)。 – NLindros

+0

maeby我应该从'wi'创建矢量'w'并且从重新构建的'Ai'创建矩阵'A',并且在这之后使用'bsxfun',但是在没有重新变形的情况下还有另一种方法吗? – user7484269

回答

0

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

如果您的矩阵的一个被存储在大小P x Q x N使得An = A(:,:,n)的3-d矩阵n = 1, 2, ..., N和您的权重被存储在大小1 x N的权重向量w则以下命令执行加权平均:

B = reshape(w*reshape(permute(A,[3,1,2]),N,[]),[P,Q]); 
+0

我的体重存储在向量中。关于矩阵存储的问题仍然打开。只能记住单元格(不是为了这个任务),并且将每个矩阵转换为矢量并将这些矢量放入另一个矩阵中。 – user7484269

+0

我编辑了我的回复。试试看看它是否适合你。 – Florian

+0

Maeby对我来说更好,但是在GPU使用时使用索引操作而不是重塑更好。感谢你的决定,它在我的任务中运行得很好。感谢, – user7484269

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) 
+0

同样的决定已经在上面写过。 – user7484269