矩阵中元素的加权和 - Matlab?
我有两个 50 x 6
矩阵,例如 A
和 B
。我想为矩阵中列的每个元素分配权重 - 对列中较早出现的元素赋予更大的权重,对同一列中较晚出现的元素赋予较小的权重......对于所有 6 列也是如此。像这样:
cumsum(weight(row)*(A(row,col)-B(row,col)); % cumsum is for cumulative sum of matrix
我们如何在不使用循环的情况下有效地完成它?
I have two 50 x 6
matrices, say A
and B
. I want to assign weights to each element of columns in matrix - more weight to elements occurring earlier in a column and less weight to elements occurring later in the same column...likewise for all 6 columns. Something like this:
cumsum(weight(row)*(A(row,col)-B(row,col)); % cumsum is for cumulative sum of matrix
How can we do it efficiently without using loops?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您将权重向量
w
作为50x1
向量,那么您可以将代码重写为BTW,我不知道为什么您有
cumsum 在循环中对标量进行操作...它没有效果。我假设您的意思是您希望对整个矩阵执行此操作。默认情况下,在矩阵上调用 cumsum 将会沿每一列进行运算。如果您需要沿行进行操作,则应使用可选的维度参数来调用它,如 cumsum(x,2)
,其中x
是您拥有的任何矩阵。If you have your weight vector
w
as a50x1
vector, then you can rewrite your code asBTW, I don't know why you have the
cumsum
operating on a scalar in a loop... it has no effect. I'm assuming that you meant that's what you wanted to do with the entire matrix. Callingcumsum
on a matrix will operate along each column by default. If you need to operate along the rows, you should call it with the optional dimension argument ascumsum(x,2)
, wherex
is whatever matrix you have.