Documentation

累积总和

Syntax

b =暨(a)
b =暨(a,dim)
b =暨(___,direction)
b =暨(___,nanflag)

Description

example

B= cumsum(A)returns the cumulative sum ofAstarting at the beginning of the first array dimension inA其大小不等于1。

  • IfA是一个向量,然后暨(A)returns a vector containing the cumulative sum of the elements ofA

  • IfA是矩阵,然后暨(A)返回包含每列的累积总和的矩阵A

  • IfA是一个多维数组,然后暨(A)acts along the第一个nonsingleton维度

example

B= cumsum(A,dim)returns the cumulative sum of the elements along dimensiondim。例如,如果A是矩阵,然后暨(a,2)returns the cumulative sum of each row.

example

B= cumsum(___,方向)可选地使用任何先前的语法指定方向。您必须指定A,并且可以选择地指定dim。For instance,暨(A,2,'reverse')returns the cumulative sum within the rows ofAby working from end to beginning of the second dimension.

example

B= cumsum(___,Nanflag)指定包括还是省略NaNvalues from the calculation for any of the previous syntaxes.暨(a,'includeenan')包括allNaN计算中的值暨(A,'omitnan')ignores them.

Examples

collapse all

Find the cumulative sum of the integers from1to5。元素B(2)is the sum ofA(1)A(2), whileB(5)is the sum of elementsA(1)throughA(5)

a = 1:5;b =暨(a)
b =1 3 6 10 15

Define a 3-by-3 matrix whose elements correspond to their linear indices.

A = [1 4 7; 2 5 8; 3 6 9]
A =1 4 7 2 5 8 3 6 9

Find the cumulative sum of the columns ofA。元素B(5)is the sum ofA(4)A(5), whileB(9)is the sum ofA(7),A(8), 和A(9)

b =暨(a)
b =1 4 7 3 9 15 6 15 24

定义一个2 x-3矩阵,其元素对应于线性索引。

A = [1 3 5; 2 4 6]
A =1 3 5 2 4 6

Find the cumulative sum of the rows ofA。元素B(3)is the sum ofA(1)A(3), whileB(5)is the sum ofA(1),A(3), 和A(5)

b =暨(a,2)
b =1 4 9 2 6 12

Create an array of logical values.

a = [true false true;true true fals]
A =2×3 logical array1 0 1 1 1 0

Find the cumulative sum of the rows ofA

b =暨(a,2)
b =1 1 2 1 2 2

The output has typedouble

class(B)
ans ='double'

Create a 3-by-3 matrix of random integers between 1 and 10.

RNGdefault;a = randi([1,10],3)
A =9 10 3 10 7 6 2 1 10

计算沿行的累积总和。指定“反向”从每行从右到左工作的选项。结果的大小与A

b =暨(a,2,“反向”)
b =22 13 3 23 13 6 13 11 10

Create a vector containingNaN值并计算累积总和。默认情况下,包括NaN值。当您包括时NaN计算中的值,累积总和变为NaNas soon as the firstNaN价值A遇到。

A = [3 5 NaN 9 0 NaN]; B = cumsum(A)
b =3 8 NaN NaN NaN NaN

You can ignoreNaNvalues in the cumulative sum calculation using the'omitnan'选项。

b =暨(a,'omitnan')
b =3 8 8 17 17 17

Input Arguments

collapse all

Input array, specified as a vector, matrix, or multidimensional array.

Data Types:double|single|int8|INT16|INT32|int64|uint8|UINT16|uint32|uint64|logical|duration
Complex Number Support:Yes

沿着操作的维度,指定为正整数标量。如果未指定值,则默认值是第一个数组维度,其大小不等于1。

考虑二维输入阵列,A:

  • 暨(a,1)在列的连续元素上工作A和returns the cumulative sums of each column.

  • 暨(a,2)在行的连续元素上工作A和returns the cumulative sums of each row.

returnsAifdim大于ndims(A)

Direction of cumulation, specified as'forward'(default) or“反向”

  • 'forward'works from1toend活动尺寸。

  • “反向”works fromendto1活动尺寸。

Data Types:char

NaN(健康)状况, specified as one of the following values:

  • 'includenan'— IncludeNaNvalues from the input when computing the cumulative sums, resulting inNaN输出中的值。

  • 'omitnan'- 忽略所有NaNvalues in the input. The sum of elements containingNaN值是所有非 - 的总和NaN元素。如果所有元素是NaN, thenreturns 0.

Data Types:char

Output Arguments

collapse all

累积总和数组,作为向量,矩阵或多维数组返回,其大小与输入数组相同A

班级B与班级相同A除非Aislogical, 在这种情况下Bisdouble

More About

collapse all

First Nonsingleton Dimension

第一个非语言尺寸是数组的第一个维度,其大小不等于1

例如:

  • IfXis a 1-by-n row vector, then the second dimension is the first nonsingleton dimension ofX

  • IfXis a 1-by-0-by-n empty array, then the second dimension is the first nonsingleton dimension ofX

  • IfX是一个1 x-1 x-3阵列,那么第三维是第一个非元素维度。X

尖端

  • MATLAB中的许多累积功能®support the“反向”选项。This option allows quick directional calculations without needing a flip or reflection of the input array.

Extended Capabilities

在R2006a之前引入

这个话题有帮助吗?