【 MATLAB 】prod 函数介绍(Product of array elements)

举报
李锐博恩 发表于 2021/07/15 06:14:08 2021/07/15
【摘要】 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 element...

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。

B = prod(A,dim)沿维度dim返回产品。 例如,如果A是矩阵,则prod(A,2)是包含每行的乘积的列向量。

 

B = prod(___,type)使用前面语法中的任何输入参数返回类型指定的类中的数组。 type可以是'double','native'或'default'。

 

B = prod(___,nanflag)指定是否在计算中包含或省略任何先前语法的NaN值.prod(A,'includenan')包括计算中的NaN值,而prod(A,'omitnan')忽略 他们。



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

 

 

文章来源: reborn.blog.csdn.net,作者:李锐博恩,版权归原作者所有,如需转载,请联系作者。

原文链接:reborn.blog.csdn.net/article/details/83245317

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。