如何在 MATLAB 中将矩阵元素除以列总和?

发布于 2024-08-12 17:46:40 字数 112 浏览 1 评论 0 原文

有没有一种简单的方法可以将每个矩阵元素除以列和?例如:

input:

1  4

4  10

output:

1/5  4/14

4/5  10/14

Is there an easy way to divide each matrix element by the column sum? For example:

input:

1  4

4  10

output:

1/5  4/14

4/5  10/14

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

神魇的王 2024-08-19 17:46:40

以下是执行此操作的不同方法的列表...

  • ...使用 bsxfun

    B = bsxfun(@rdivide,A,sum(A));
    
  • ...使用repmat

    B = A./repmat(sum(A),size(A,1),1);
    
  • ...使用外部产品(按照建议通过Amro):

    B = A./(ones(size(A,1),1)*sum(A));
    
  • ...并使用 for 循环(如 mtrw):

    <前><代码>B = A;
    列总和 = sum(B);
    对于 i = 1:numel(columnSums)
    B(:,i) = B(:,i)./columnSums(i);
    结尾

更新:

从 MATLAB R2016b 及更高版本开始,大多数内置二进制函数(列表可以在 此处)支持隐式扩展,这意味着它们具有 bsxfun。因此,在最新的 MATLAB 版本中,您所要做的就是:

B = A./sum(A);

Here's a list of the different ways to do this ...

  • ... using bsxfun:

    B = bsxfun(@rdivide,A,sum(A));
    
  • ... using repmat:

    B = A./repmat(sum(A),size(A,1),1);
    
  • ... using an outer product (as suggested by Amro):

    B = A./(ones(size(A,1),1)*sum(A));
    
  • ... and using a for loop (as suggested by mtrw):

    B = A;
    columnSums = sum(B);
    for i = 1:numel(columnSums)
      B(:,i) = B(:,i)./columnSums(i);
    end
    

Update:

As of MATLAB R2016b and later, most built-in binary functions (list can be found here) support implicit expansion, meaning they have the behavior of bsxfun by default. So, in the newest MATLAB versions, all you have to do is:

B = A./sum(A);
半城柳色半声笛 2024-08-19 17:46:40
a=[1 4;4 10]
a =
     1     4
     4    10

a*diag(1./sum(a,1))
ans =
    0.2000    0.2857
    0.8000    0.7143
a=[1 4;4 10]
a =
     1     4
     4    10

a*diag(1./sum(a,1))
ans =
    0.2000    0.2857
    0.8000    0.7143
池予 2024-08-19 17:46:40

无法抗拒尝试列表理解。如果这个矩阵以行主列表的形式表示,请尝试以下操作:

>>> A = [[1,4],[4,10]]
>>> [[float(i)/j for i,j in zip(a,map(sum,zip(*A)))] for a in A]
[[0.20000000000000001, 0.2857142857142857], [0.80000000000000004, 0.7142857142857143]]

是的,我知道这不是超级高效,因为我们每行计算一次列总和。将其保存在名为 colsums 的变量中,如下所示:

>>> colsums = map(sum,zip(*A))
>>> [[float(i)/j for i,j in zip(a,colsums)] for a in A]
[[0.20000000000000001, 0.2857142857142857], [0.80000000000000004, 0.7142857142857143]]

请注意,zip(*A) 给出转置(A)。

Couldn't resist trying a list comprehension. If this matrix was represented in a row-major list of lists, try this:

>>> A = [[1,4],[4,10]]
>>> [[float(i)/j for i,j in zip(a,map(sum,zip(*A)))] for a in A]
[[0.20000000000000001, 0.2857142857142857], [0.80000000000000004, 0.7142857142857143]]

Yes, I know that this is not super-efficient, as we compute the column sums once per row. Saving this in a variable named colsums looks like:

>>> colsums = map(sum,zip(*A))
>>> [[float(i)/j for i,j in zip(a,colsums)] for a in A]
[[0.20000000000000001, 0.2857142857142857], [0.80000000000000004, 0.7142857142857143]]

Note that zip(*A) gives transpose(A).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文