【问题标题】:How get mean of weighted matrixes without loop?如何获得没有循环的加权矩阵的平均值?
【发布时间】:2017-02-04 18:08:00
【问题描述】:

我有一组标量和矩阵:

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

如何获得

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

没有循环? 以及获取效率如何

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

其中bici 是向量,而bi*ci 是矩阵,而不是标量?

【问题讨论】:

  • 您是否尝试将您写的内容(如w1*A1 + w2*A2 + ... + wn*An)输入到Matlab?这是基本的 Matlab 矩阵运算:timesplus
  • maeby 我应该从 wi 创建向量 w 和矩阵 A 从改造后的 Ai 并在此之后使用 bsxfun 但是否有另一种不重塑的方法?

标签: matlab matrix matrix-multiplication vector-multiplication


【解决方案1】:

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

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

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

【讨论】:

  • 我的体重存储在向量中。关于矩阵存储的问题仍然存在。只能记住单元格(不适用于此任务)以及将每个矩阵转换为向量并将此向量放入另一个矩阵中。
  • 我已经编辑了我的回复。试试看它是否适合你。
  • Maeby 对我来说更好,但在使用 GPU 时使用索引操作而不是重塑似乎更好。感谢您的决定,它在我的任务中足够好。
【解决方案2】:

第一个问题可以通过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)

【讨论】:

    猜你喜欢
    • 2016-02-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-23
    • 1970-01-01
    • 2013-06-06
    • 1970-01-01
    • 2020-02-16
    相关资源
    最近更新 更多