Documentation

prod

Product of array elements

Syntax

B = prod(A)
B = prod(A,dim)
B = prod(___,type)
B = prod(___,nanflag)

描述

example

B= prod(A)returns the product of the array elements ofA.

  • IfAis a vector, thenprod(A)returns the product of the elements.

  • IfAis a nonempty matrix, thenprod(A)treats the columns ofAas vectors and returns a row vector of the products of each column.

  • IfAis an empty 0-by-0 matrix,prod(A)returns1.

  • IfAis a multidimensional array, thenprod(A)acts along thefirst nonsingleton dimensionand returns an array of products. The size of this dimension reduces to1while the sizes of all other dimensions remain the same.

prodcomputes and returnsBassingle当输入时,A, issingle. For all other numeric and logical data types,prodcomputes and returnsBasdouble.

example

B= prod(A,dim)returns the products along dimensiondim. For example, ifAis a matrix,prod(A,2)is a column vector containing the products of each row.

example

B= prod(___,type)returns an array in the class specified bytype, using any of the input arguments in the previous syntaxes.typecan be'double','native', or'default'.

example

B= prod(___,nanflag)specifies whether to include or omitNaNvalues from the calculation for any of the previous syntaxes.prod(A,'includenan')includesNaNvalues in the calculation whileprod(A,'omitnan')ignores them.

Examples

collapse all

Create a 3-by-3 array whose elements correspond to their linear indices.

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

Find the product of the elements in each column. The length of the first dimension is 1, and the length of the second dimension matchessize(A,2).

B = prod(A)
B =6 120 504

Create an array of logical values.

A = [true false; true true]
A =2x2 logical array1 0 1 1

Find the product of the elements in each column.

B = prod(A)
B =1 0

The output has typedouble.

class(B)
ans = 'double'

Create a 3-by-3 array whose elements correspond to their linear indices.

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

Find the product of the elements in each row and reduce the length of the second dimension to 1. The length of the first dimension matchessize(A,1), and the length of the second dimension is 1.

dim = 2; B = prod(A,dim)
B =28 80 162

Create a 3-by-3-by-2 array whose elements correspond to their linear indices.

A=[1:3:7;2:3:8;3:3:9]; A(:,:,2)=[10:3:16;11:3:17;12:3:18]
A = A(:,:,1) = 1 4 7 2 5 8 3 6 9 A(:,:,2) = 10 13 16 11 14 17 12 15 18

Find the product of each element in the first plane with its corresponding element in the second plane. The length of the first dimension matchessize(A,1), the length of the second dimension matchessize(A,2), and the length of the third dimension is 1.

dim = 3; B = prod(A,dim)
B =10 52 112 22 70 136 36 90 162

Create a 3-by-3 array of single-precision values.

A = single([1200 1500 1800; 1300 1600 1900; 1400 1700 2000])
A =3x3 single matrix1200 1500 1800 1300 1600 1900 1400 1700 2000

Find the product of the elements in each row by multiplying in double precision.

B = prod(A,2,'double')
B =1.0e+09 * 3.2400 3.9520 4.7600

The output is double precision.

class(B)
ans = 'double'

Create a 3-by-3 array of 8-bit unsigned integers.

A = uint8([1:3:7;2:3:8;3:3:9])
A =3x3 uint8 matrix1 4 7 2 5 8 3 6 9

Find the product of the elements in each column natively inuint8.

B = prod(A,'native')
B =1x3 uint8 row vector6 120 255

The result is an array of 8-bit unsigned integers.

class(B)
ans = 'uint8'

Create a vector and compute its product, excludingNaNvalues. If you do not specify'omitnan', thenprod(A)returnsNaN.

A = [1 3 2 4 NaN 3 NaN 2]; P = prod(A,'omitnan')
P= 144

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
Complex Number Support:Yes

Dimension to operate along, specified as a positive integer scalar. If no value is specified, then the default is the first array dimension whose size does not equal 1.

Dimensiondimindicates the dimension whose length reduces to1. Thesize(B,dim)is1, while the sizes of all other dimensions remain the same.

Consider a two-dimensional input array,A.

  • Ifdim = 1, thenprod(A,1)returns a row vector containing the product of the elements in each column.

  • Ifdim = 2, thenprod(A,2)returns a column vector containing the product of the elements in each row.

prodreturnsAwhendimis greater thanndims(A).

Data Types:double|single|int8|int16|int32|int64|uint8|uint16|uint32|uint64

Output class, specified as'default','double', or'native', and which defines the data type of the output,B.

type Output data type
'default' double, unless the input data type issingle. In which case, the output data type issingle.
'double' double
'native' same data type as the input array,A

NaNcondition, specified as one of these values:

  • 'includenan'— IncludeNaNvalues from the input when computing the product, resulting inNaNoutput.

  • 'omitnan'— IgnoreNaNvalues in the input. If all elements areNaN, thenprodreturns 1.

Output Arguments

collapse all

Product array, returned as a scalar, vector, matrix, or multidimensional array.

The class ofBis as follows:

  • If thetypeargument specifies'default'or is not used

    • and the input is notsingle, then the output isdouble.

    • and the input issingle, then the output issingle.

  • If thetypeargument specifies'double', then the output isdoubleregardless of the input data type.

  • If thetypeargument specifies'native', then the output is the same data type as the input.

More About

collapse all

First Nonsingleton Dimension

The first nonsingleton dimension is the first dimension of an array whose size is not equal to1.

For example:

  • 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.

  • IfXis a 1-by-1-by-3 array, then the third dimension is the first nonsingleton dimension ofX.

Extended Capabilities

See Also

|||

Introduced before R2006a

Was this topic helpful?