1. 程式人生 > >【 MATLAB 】prod 函式介紹(Product of array elements)

【 MATLAB 】prod 函式介紹(Product of array elements)

prod

Product of array elements

Syntax

B = prod(A)

B = prod(A,dim)

B = prod(___,type)

B = prod(___,nanflag)

Description

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.

B = prod(A) 返回A的陣列元素的乘積。

如果A是向量,則prod(A)返回元素的乘積。

如果A是非空矩陣,那麼prod(A)將A的列視為向量並返回每列乘積的行向量。

如果A是空的0乘0矩陣,則prod(A)返回1。

如果A是多維陣列,那麼prod(A)沿著第一個非單體維度行動並返回一個產品陣列。 此尺寸的尺寸減小到1,而所有其他尺寸的尺寸保持不變。

First Nonsingleton Dimension

The first nonsingleton dimension is the first dimension of an array whose size is not equal to 1

.

第一個非單體維度是陣列的第一個維度,其大小不等於1。

For example:

  • 如果X是1乘n的行向量,則第二個維度是X的第一個非單獨維度。

  • 如果X是1乘0乘n的空陣列,則第二維是X的第一個非單體維數。

  • 如果X是1乘1乘3的陣列,則第三維是X的第一個非單體維數。

當輸入A為single型別時,prod計算並返回B為single。 對於所有其他數字和邏輯資料型別,prod計算並將B返回為double。

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 = 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).

查詢每列中元素的乘積。 第一個維度的長度為1,第二個維度的長度與 size(A,2)匹配。

B = prod(A)
B = 1×3

     6   120   504

Product of Logical Input

Create an array of logical values.

A = [true false; true true]
A = 2x2 logical array

   1   0
   1   1

Find the product of the elements in each column.

B = prod(A)
B = 1×2

     1     0

The output has type 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 = 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.

dim = 2;
B = prod(A,dim)
B = 3×1

    28
    80
   162

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 = 
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 matches size(A,1), the length of the second dimension matches size(A,2), and the length of the third dimension is 1.

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

    10    52   112
    22    70   136
    36    90   162

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 = 3x3 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 = prod(A,2,'double')
B = 3×1
109 ×

    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 = 3x3 uint8 matrix

   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 = 1x3 uint8 row vector

     6   120   255

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

class(B)
ans = 
'uint8'

Product Excluding NaN

Create a vector and compute its product, excluding NaN values. If you do not specify 'omitnan', then prod(A)returns NaN.

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