Why is sum(X, 1) the sum of the columns in MATLAB?

前端 未结 4 1795
名媛妹妹
名媛妹妹 2021-02-19 03:46
>> X = [0 1 2
        3 4 5]

>> sum(X, 1)

ans =

     3     5     7

sum(X, 1) should sum along the 1st dimensio

相关标签:
4条回答
  • 2021-02-19 04:23

    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.

    0 讨论(0)
  • 2021-02-19 04:32

    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 !

    0 讨论(0)
  • 2021-02-19 04:35

    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.

    0 讨论(0)
  • 2021-02-19 04:40

    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.

    0 讨论(0)
提交回复
热议问题