为什么sum(X,1)在MATLAB中是列的总和? [英] Why is sum(X, 1) the sum of the columns in MATLAB?
问题描述
>> X = [0 1 2
3 4 5]
>> sum(X, 1)
ans =
3 5 7
sum(X, 1)
应该沿1st
维度(行)求和,如文档所述:
sum(X, 1)
should sum along the 1st
dimension(row) as per the document says:
S = SUM(X,DIM)沿 尺寸为DIM.
S = SUM(X,DIM) sums along the dimension DIM.
但是为什么它实际上沿2nd
维(列)求和?
But why does it actually sums along the 2nd
dimension(column)?
推荐答案
在我看来,它与其他所有内容完全一致.
In my opinion, it is perfectly consistent with everything else.
sum(A,dim)
沿尺寸dim的方向沿求和.
sum(A,dim)
sums along the direction of dimension dim.
行被计为向下",因此sum(A,1)
总计为向下".列是在右边"计算的,因此sum(A,2)
在右边"求和.
Rows are counted "down", so sum(A,1)
sums "down". Columns are counted "to the right", so sum(A,2)
sums "to the right".
另一种查看方式是sum(A,dim)
通过求和将维度dim
折叠为1.因此,沿维度1求和的4x3数组会使第一个维度崩溃,从而导致1x3数组.
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.
这篇关于为什么sum(X,1)在MATLAB中是列的总和?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!