MATLAB-sum function usage

Source: Internet
Author: User

MATLAB --- sum function usage

1/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
Result is double (210) while sum (x, 'native ') accumulates in
Int8, but overflows and saturates to int8 (127 ).
 
2/
Syntax
B = sum ()
B = sum (A, dim)
Function Description
B = sum ()
Returns the values of different spatial dimensions in the array. If a is a vector, sum (a) returns the element and sum of the vector. If a is a matrix, sum (a) uses the column vector in A as the base vector and returns the row vectors of each element of the base vector of each column. If a is a multi-dimensional array, sum (a) uses the data along the first non-unit as the vector and returns an array of row vectors.
B = sum (A, dim)
Sums the elements of matrix A along the dimension specified by the scalar dim.
 
3/
>

A =

3 4 2
1 5 3
4 7 1

> Sum ()

Ans =

8 16 6

> Sum (A, 2)

Ans =

9
9
12

> Sum (A, 1)

Ans =

8 16 6

> A (:, 2)

Ans =

4
5
7

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.