为什么 sum(X, 1) 是 MATLAB 中各列的总和?
>> 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
在我看来,它与其他一切完全一致。
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", sosum(A,2)
sums "to the right".Another way to look at this is that
sum(A,dim)
collapses dimensiondim
to 1 by taking the sum. Thus, a 4x3 array summed along dimension 1 collapses the first dimension, leading to a 1x3 array.http://www.mathworks.com/access/helpdesk/帮助/techdoc/ref/sum.html
你的猜测和我的一样好。
http://www.mathworks.com/access/helpdesk/help/techdoc/ref/sum.html
Your guess is as good as mine.
1 表示列,
根据 http://www.mathworks.com/access/帮助台/help/techdoc/ref/sum.html
1 means column,
according to http://www.mathworks.com/access/helpdesk/help/techdoc/ref/sum.html
我认为 Matlab 文档对此非常清楚。它指出:
欢迎您认为 Matlab 是错了,但不会改变!
I think that the Matlab documentation on this is quite clear. It states:
You're welcome to think that Matlab is wrong, but it ain't going to change !