1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<HTML>
<HEAD>
<TITLE>SUM Sum Function
</TITLE>
</HEAD>
<BODY>
<H2>SUM Sum Function
</H2>
<P>
Section: <A HREF=sec_elementary.html> Elementary Functions </A>
<H3>Usage</H3>
Computes the summation of an array along a given dimension. The general
syntax for its use is
<PRE>
y = sum(x,d)
</PRE>
<P>
where <code>x</code> is an <code>n</code>-dimensions array of numerical type.
The output is of the same numerical type as the input. The argument
<code>d</code> is optional, and denotes the dimension along which to take
the summation. The output <code>y</code> is the same size as <code>x</code>, except
that it is singular along the summation direction. So, for example,
if <code>x</code> is a <code>3 x 3 x 4</code> array, and we compute the summation along
dimension <code>d=2</code>, then the output is of size <code>3 x 1 x 4</code>.
<H3>Function Internals</H3>
The output is computed via
<P>
<DIV ALIGN="CENTER">
<IMG SRC="sum_eqn1.png">
</DIV>
<P>
If <code>d</code> is omitted, then the summation is taken along the
first non-singleton dimension of <code>x</code>.
<H3>Example</H3>
The following piece of code demonstrates various uses of the summation
function
<PRE>
--> A = [5,1,3;3,2,1;0,3,1]
A =
5 1 3
3 2 1
0 3 1
</PRE>
<P>
We start by calling <code>sum</code> without a dimension argument, in which
case it defaults to the first nonsingular dimension (in this case,
along the columns or <code>d = 1</code>).
<PRE>
--> sum(A)
ans =
8 6 5
</PRE>
<P>
Next, we take the sum along the rows.
<PRE>
--> sum(A,2)
ans =
9
6
4
</PRE>
<P>
</BODY>
</HTML>
|