Accelerating the pace of engineering and science

# Documentation Center

• Trial Software

# prod

Product of array elements

## Description

example

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

• If A is a vector, then prod(A) returns the product of the elements.

• If A is a nonempty matrix, then prod(A) treats the columns of A as vectors and returns a row vector of the products of each column.

• If A is an empty 0-by-0 matrix, prod(A) returns 1.

• If A is a multidimensional array, then prod(A) acts along the first nonsingleton dimension and returns an array of products. The size of this dimension reduces to 1 while the sizes of all other dimensions remain the same.

prod computes and returns B as single if the input, A, is single. For all other numeric and logical data types, prod computes and returns B as double.

example

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

example

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

## Examples

expand all

### Product of Elements in Each Column

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.

`B = prod(A)`
```B =

6   120   504
```

The length of the first dimension is 1 , and the length of the second dimension matches size(A,2).

### Logical Input with Double Output

Create an array of logical values.

`A = [true false; true true]`
```A =

1     0
1     1
```

Find the product of the elements in each column.

`B = prod(A)`
```B =

1     0```

The output is double.

`class(B)`
```ans =

double
```

### Product of Elements in Each Row

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.

```dim = 2;
B = prod(A,dim)```
```B =

28
80
162```

The length of the first dimension matches size(A,1), and the length of the second dimension is 1.

### Product of Elements in Each Plane

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(:,:,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.

```dim = 3;
B = prod(A,dim)```
```B =

10    52   112
22    70   136
36    90   162
```

The length of the first dimension matches size(A,1), the length of the second dimension matches size(A,2), and the length of the third dimension is 1.

### Single-Precision Input treated as Double

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

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

1200        1500        1800
1300        1600        1900
1400        1700        2000
```

Find the product of the elements in each row by performing the multiplication 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```

### Integer Data Type for Input and Output

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

`A = uint8([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 natively in uint8.

`B = prod(A,'native')`
```B =

6  120  255

```

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

`class(B)`
```ans =

uint8
```

## Input Arguments

expand all

### A — Input arrayvector | matrix | multidimensional array

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

### dim — Dimension to operate alongpositive integer scalar

Dimension to operate along, specified as a positive integer scalar. If no value is specified, 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, then prod(A,1) returns a row vector containing the product of the elements in each column.

• If dim = 2, then prod(A,2) returns a column vector containing the product of the elements in each row.

prod returns A if dim is greater than ndims(A).

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

### type — Output class'double' | 'native'

Output class, specified as 'double' or 'native', defines the data type that the operation is performed in and output in.

• If type is 'double', then prod computes and returns a double precision array, regardless of the input data type. For example, if A is single, then prod multiplies in and returns in double.

• If type is 'native', prod multiplies natively and returns an array with the same data type as the input array A. For example, if A is an 8-bit unsigned integer, prod multiplies natively and returns B with data type uint8.

Data Types: char

## Output Arguments

expand all

### B — Product arrayscalar | vector | matrix | multidimensional array

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

The class of B is as follows:

• If the type argument is not used and the input is not single, then the output is double.

• If the type argument is not used and the input is single, then the output is single.

• If the type argument specifies 'double', then the output is double regardless of the input data type.

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

expand all

### First Nonsingleton Dimension

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 of X.

• If X is a 1-by-0-by-n empty array, then the second dimension is the first nonsingleton dimension of X.

• If X is a 1-by-1-by-3 array, then the third dimension is the first nonsingleton dimension of X.