为什么 sum(X, 1) 是 MATLAB 中各列的总和?

发布于 2024-08-29 06:45:25 字数 299 浏览 5 评论 0原文

>> X = [0 1 2
        3 4 5]

>> sum(X, 1)

ans =

     3     5     7

sum(X, 1) 应按照文档所述沿 1st 维度()求和:

S = SUM(X,DIM) 沿 尺寸 DIM。

但为什么它实际上是沿着第二维度()求和的呢?

>> X = [0 1 2
        3 4 5]

>> sum(X, 1)

ans =

     3     5     7

sum(X, 1) should sum along the 1st dimension(row) as per the document says:

S = SUM(X,DIM) sums along the
dimension DIM.

But why does it actually sums along the 2nd dimension(column)?

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

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

发布评论

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

评论(4

奶茶白久 2024-09-05 06:45:25

在我看来,它与其他一切完全一致。

sum(A,dim) 沿维度 dim 的方向求和。

行按“向下”计数,因此 sum(A,1) 求和“向下”。列按“向右”计数,因此 sum(A,2) 求和“向右”。

另一种看待此问题的方法是,sum(A,dim) 通过求和将维度 dim 折叠为 1。因此,沿维度 1 求和的 4x3 数组会折叠第一维度,从而得到 1x3 数组。

In my opinion, it is perfectly consistent with everything else.

sum(A,dim) sums along the direction of dimension dim.

Rows are counted "down", so sum(A,1) sums "down". Columns are counted "to the right", so sum(A,2) sums "to the right".

Another way to look at this is that sum(A,dim) collapses dimension dim to 1 by taking the sum. Thus, a 4x3 array summed along dimension 1 collapses the first dimension, leading to a 1x3 array.

岁月打碎记忆 2024-09-05 06:45:25

http://www.mathworks.com/access/helpdesk/帮助/techdoc/ref/sum.html

B = sum(A,dim) 沿
由标量指定的 A 的维度
暗淡。暗淡输入是一个整数值
从 1 到 N,其中 N 是
A 中的尺寸。 将 dim 设置为 1 以
计算每列的总和,2 到
对行求和等

你的猜测和我的一样好。

http://www.mathworks.com/access/helpdesk/help/techdoc/ref/sum.html

B = sum(A,dim) sums along the
dimension of A specified by scalar
dim. The dim input is an integer value
from 1 to N, where N is the number of
dimensions in A. Set dim to 1 to
compute the sum of each column, 2 to
sum rows, etc.

Your guess is as good as mine.

苹果你个爱泡泡 2024-09-05 06:45:25

1 表示列,
根据 http://www.mathworks.com/access/帮助台/help/techdoc/ref/sum.html

B = sum(A,dim) 沿
由标量指定的 A 的维度
暗淡。
dim 输入是从 1 到 N 的整数值,其中 N 是
A 中的尺寸。
将 dim 设置为 1 来计算每列的总和,设置为 2 来计算行的总和,等等

1 means column,
according to http://www.mathworks.com/access/helpdesk/help/techdoc/ref/sum.html

B = sum(A,dim) sums along the
dimension of A specified by scalar
dim.
The dim input is an integer value from 1 to N, where N is the number of
dimensions in A.
Set dim to 1 to compute the sum of each column, 2 to sum rows, etc.

杀お生予夺 2024-09-05 06:45:25

我认为 Matlab 文档对此非常清楚。它指出:

B = sum(A,dim) 沿标量 dim 指定的 A 维度求和。 dim 输入是从 1 到 N 的整数值,其中 N 是 A 中的维度数。将 dim 设置为 1 可以计算每列的总和,设置为 2 可以计算行的总和,等等。

欢迎您认为 Matlab 是错了,但不会改变!

I think that the Matlab documentation on this is quite clear. It states:

B = sum(A,dim) sums along the dimension of A specified by scalar dim. The dim input is an integer value from 1 to N, where N is the number of dimensions in A. Set dim to 1 to compute the sum of each column, 2 to sum rows, etc.

You're welcome to think that Matlab is wrong, but it ain't going to change !

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