MATLAB—sum函數用法

來源:互聯網
上載者:User

MATLAB---sum函數用法

1/  SUM Sum of elements.
    S = SUM(X) is the sum of the elements of the vector X. If
    X is a matrix, S is a row vector with the sum over each
    column. For N-D arrays, SUM(X) operates along the first
    non-singleton dimension.
    If X is floating point, that is double or single, S is
    accumulated natively, that is in the same class as X,
    and S has the same class as X. If X is not floating point,
    S is accumulated in double and S has class double.
 
    S = SUM(X,DIM) sums along the dimension DIM.
 
    S = SUM(X,'double') and S = SUM(X,DIM,'double') accumulate
    S in double and S has class double, even if X is single.
 
    S = SUM(X,'native') and S = SUM(X,DIM,'native') accumulate
    S natively and S has the same class as X.
 
    Examples:
    If   X = [0 1 2
              3 4 5]
 
    then sum(X,1) is [3 5 7] and sum(X,2) is [ 3
                                              12];
 
    If X = int8(1:20) then sum(X) accumulates in double and the
    result is double(210) while sum(X,'native') accumulates in
    int8, but overflows and saturates to int8(127).
 
2/
文法
B=sum(A)
B=sum(A,dim)
函數描述
B=sum(A)
返回數組中各個不同空間維的數值和,若A為一個向量,sum(A)將A返迴向量元素和。若A為一個矩陣,sum(A)將A中列向量作為基向量,返回每一列基向量各元素和的行向量。若A為一個多維陣列,sum(A)將沿第一個非單位的資料作為向量,返回一個行向量數組。
B=sum(A,dim)
沿由標量dim指定的維對矩陣A的元素求和。
 
3/
>> A

A =

     3     4     2
     1     5     3
     4     7     1

>> sum(A)

ans =

     8    16     6

>> sum(A,2)

ans =

     9
     9
    12

>> sum(A,1)

ans =

     8    16     6

>> A(:,2)

ans =

     4
     5
     7

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.