prod - Product of array elements - MATLAB (original) (raw)
Product of array elements
Syntax
Description
[B](#btp6ofm-1-B) = prod([A](#btp6ofm-1-A))
returns the product of the array elements of A
.
- If
A
is a vector, thenprod(A)
returns the product of the elements. - If
A
is a nonempty matrix, thenprod(A)
treats the columns ofA
as vectors and returns a row vector of the products of each column. - If
A
is an empty 0-by-0 matrix,prod(A)
returns1
. - If
A
is a multidimensional array, thenprod(A)
acts along the first nonsingleton dimension and returns an array of products. The size ofB
in this dimension reduces to1
, while the sizes of all other dimensions remain the same as inA
. - If
A
is a table or timetable, thenprod(A)
returns a one-row table of the products of each variable. (since R2023a)
prod
computes and returns B
assingle
when the input, A
, issingle
. For all other numeric and logical data types,prod
computes and returns B
asdouble
.
[B](#btp6ofm-1-B) = prod([A](#btp6ofm-1-A),`"all"`)
returns the product of all elements of A
.
[B](#btp6ofm-1-B) = prod([A](#btp6ofm-1-A),[dim](#btp6ofm-1-dim))
returns the product along dimension dim
. For example, ifA
is a matrix, prod(A,2)
is a column vector containing the products of each row.
[B](#btp6ofm-1-B) = prod([A](#btp6ofm-1-A),[vecdim](#mw%5Ff733e5e9-9595-4a56-ba26-cfbf09d1e828))
returns the product based on the dimensions specified in the vectorvecdim
. For example, if A
is a matrix, then prod(A,[1 2])
returns the product of all elements inA
because every element of a matrix is contained in the array slice defined by dimensions 1 and 2.
[B](#btp6ofm-1-B) = prod(___,[outtype](#btp6ofm-1-type))
returns an array in the class specified by outtype
, using any of the input arguments in the previous syntaxes. outtype
can be "double"
, "native"
, or"default"
.
[B](#btp6ofm-1-B) = prod(___,[nanflag](#mw%5F8180f565-52c4-4ec5-a01b-e2b72a881cdd))
specifies whether to include or omit NaN
values inA
. For example, prod(A,"omitnan")
ignores NaN
values when computing the product. By default,prod
includes NaN
values.
Examples
Create a 3-by-3 array whose elements correspond to their linear indices.
A = 3×3
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 matches size(A,2)
.
Create an array of logical values.
A = [true false; true true]
A = 2×2 logical array
1 0 1 1
Find the product of the elements in each column.
The output has type double
.
Create a 3-by-3 array whose elements correspond to their linear indices.
A = 3×3
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 matches size(A,1)
, and the length of the second dimension is 1.
Create a 3-D array and compute the product over each page of data (rows and columns).
A(:,:,1) = [2 4; -2 1]; A(:,:,2) = [1 2; -5 3]; A(:,:,3) = [4 4; 1 -3]; B1 = prod(A,[1 2])
B1 = B1(:,:,1) =
-16
B1(:,:,2) =
-30
B1(:,:,3) =
-48
To compute the product over all dimensions of an array, you can either specify each dimension in the vector dimension argument, or use the "all"
option.
Create a 3-by-3 array of single-precision values.
A = single([1200 1500 1800; 1300 1600 1900; 1400 1700 2000])
A = 3×3 single matrix
1200 1500 1800
1300 1600 1900
1400 1700 2000
Find the product of the elements in each row by multiplying in double precision.
B = 3×1 109 ×
3.2400
3.9520
4.7600
The output is double precision.
Create a 3-by-3 array of 8-bit unsigned integers.
A = uint8([1:3:7;2:3:8;3:3:9])
A = 3×3 uint8 matrix
1 4 7 2 5 8 3 6 9
Find the product of the elements in each column natively in uint8
.
B = 1×3 uint8 row vector
6 120 255
The result is an array of 8-bit unsigned integers.
Create a matrix containing NaN
values.
A = [1.77 -0.005 NaN -2.95; NaN 0.34 NaN 0.19]
A = 2×4
1.7700 -0.0050 NaN -2.9500
NaN 0.3400 NaN 0.1900
Compute the products of the matrix, excluding NaN
values. For matrix column that contain any NaN
value, prod
computes with the non-NaN
elements. For matrix columns that contain all NaN
values, the product is 1.
B = 1×4
1.7700 -0.0017 1.0000 -0.5605
Input Arguments
Input array, specified as a vector, matrix, multidimensional array, table, or timetable.
Data Types: double
| single
| int8
| int16
| int32
| int64
| uint8
| uint16
| uint32
| uint64
| logical
| table
| timetable
Complex Number Support: Yes
Dimension to operate along, specified as a positive integer scalar. If you do not specify the dimension, then the default is the first array dimension whose size does not equal 1.
Dimension dim
indicates the dimension whose length reduces to 1
. The size(B,dim)
is 1
, while the sizes of all other dimensions remain the same.
Consider a two-dimensional input array, A
.
- If
dim = 1
, thenprod(A,1)
returns a row vector containing the product of the elements in each column. - If
dim = 2
, thenprod(A,2)
returns a column vector containing the product of the elements in each row.
prod
returns A
when dim
is greater than ndims(A)
.
Data Types: double
| single
| int8
| int16
| int32
| int64
| uint8
| uint16
| uint32
| uint64
Vector of dimensions, specified as a vector of positive integers. Each element represents a dimension of the input array. The lengths of the output in the specified operating dimensions are 1, while the others remain the same.
Consider a 2-by-3-by-3 input array, A
. Thenprod(A,[1 2])
returns a 1-by-1-by-3 array whose elements are the products of each page of 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
.
outtype | Output data type |
---|---|
"default" | double, unless the input data type issingle, table, ortimetable. In which case, the output data type is single ortable, respectively. |
"double" | double, unless the input data type istable ortimetable. In which case, the output data type is table. |
"native" | Same data type as the input array, A, unless the input data type is timetable. In which case, the output data type istable. |
Missing value condition, specified as one of these values:
"includemissing"
or"includenan"
— IncludeNaN
values inA
when computing the product. If any element in the operating dimension isNaN
, then the corresponding element inB
isNaN
."includemissing"
and"includenan"
have the same behavior."omitmissing"
or"omitnan"
— IgnoreNaN
values inA
, and compute the product over fewer points. If all elements in the operating dimension areNaN
, then the corresponding element inB
is 1."omitmissing"
and"omitnan"
have the same behavior.
Output Arguments
Product array, returned as a scalar, vector, matrix, multidimensional array, or table.
The class of B
is as follows:
- If the
outtype
argument specifies"default"
or is not used- and the input is not
single
,table
, ortimetable
, then the output isdouble
. - and the input is
single
, then the output issingle
. - and the input is
table
ortimetable
, then the output istable
.
- and the input is not
- If the
outtype
argument specifies"double"
, then the output isdouble
regardless of the input data type, unless the input istable
ortimetable
. - If the
outtype
argument specifies"native"
, then the output is the same data type as the input, unless the input istimetable
. In which case, the output istable
.
More About
The first nonsingleton dimension is the first dimension of an array whose size is not equal to 1
.
For example:
- If
X
is a 1-by-n row vector, then the second dimension is the first nonsingleton dimension ofX
. - If
X
is a 1-by-0-by-n empty array, then the second dimension is the first nonsingleton dimension ofX
. - If
X
is a 1-by-1-by-3 array, then the third dimension is the first nonsingleton dimension ofX
.
Extended Capabilities
Theprod
function fully supports tall arrays. For more information, see Tall Arrays.
Usage notes and limitations:
- If used, the arguments
dim
,vecdim
,outtype
, andnanflag
must be constant at code generation time. - For input argument
A
:- If you do not specify a dimension, the code generator operates along the first dimension of the input array that is variable size or whose size does not equal 1. If this dimension is variable size at code generation time and is 1 at run time, a run-time error can occur. To avoid this error, specify the dimension.
- If all dimensions of the input array are variable size at code generation time, this array cannot be empty at run time.
Usage notes and limitations:
- If you supply
dim
, it must be a constant.
The prod
function supports GPU array input with these usage notes and limitations:
- The order of the products in
prod
operation is not defined. Therefore, theprod
operation on a GPU array might not return exactly the same answer as theprod
operation on the corresponding numeric array.
For more information, see Run MATLAB Functions on a GPU (Parallel Computing Toolbox).
Usage notes and limitations:
- The order of the products in
prod
operation is not defined. Therefore, theprod
operation on a distributed array might not return exactly the same answer as theprod
operation on the corresponding numeric array. The difference might be significant whenA
is a signed integer type and its product is accumulated natively.
For more information, see Run MATLAB Functions with Distributed Arrays (Parallel Computing Toolbox).
Version History
Introduced before R2006a
Include or omit missing values in the input array when computing the product by using the "includemissing"
or "omitmissing"
options. These options have the same behavior as the "includenan"
and "omitnan"
options, respectively.
The prod
function can calculate on all variables within a table or timetable without indexing to access those variables. All variables must have data types that support the calculation. For more information, see Direct Calculations on Tables and Timetables.
Operate on multiple dimensions of the input array at a time. Specify a vector of operating dimensions, or specify the "all"
option to operate on all array dimensions.