找回密码
 注册
关于网站域名变更的通知
查看: 1066|回复: 1
打印 上一主题 下一主题

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

[复制链接]

该用户从未签到

跳转到指定楼层
1#
发表于 2020-1-13 09:48 | 只看该作者 |只看大图 回帖奖励 |倒序浏览 |阅读模式

EDA365欢迎您登录!

您需要 登录 才可以下载或查看,没有帐号?注册

x
$ ?8 h' v& V. Q! h' n8 T, {
prod

* D) x  h* h0 z* D3 x3 I  HProduct of array elements* v& J- z2 I. z

( ]/ b$ X2 a' z, l' f. ^& E  W1 p# E2 l6 G0 g8 d8 h
Syntax) O6 ?) b! P( Q8 Q. e  l/ n

1 z! R+ J( j9 D& t  aB = prod(A); Z- O8 H) i; v. j! O) O

3 j6 f2 _, a( N  SB = prod(A,dim)& u+ p9 r- f# P0 \6 m8 K0 R

  p6 Q( c1 O/ }3 ]8 z4 @B = prod(___,type)0 s1 P- N. R" p3 N! K# B7 I7 L! ]

: e) e6 }. D% c5 U' b- JB = prod(___,nanflag)
. j8 U- ^+ ]; s9 |# W. s9 B% _3 P+ B7 W7 f$ N+ c- l3 S
5 H: a6 ^2 S, C1 w  [
Description/ Z  g+ D: ]# t4 n* T5 U" Z. v

! N; W" u7 T" q6 `9 m% s+ u' m% VB = prod(A) returns the product of the array elements of A.
+ l$ z  j5 p5 n( f
8 |0 t+ G# ^% }2 Y( H; Y
  • 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.
    7 P* O1 T6 I+ S* F( x) m, o

7 B; q# T% W2 n8 SB = prod(A) 返回A的数组元素的乘积。; h- n5 u& C& ^* y" Q
" R) g1 {/ j! q; r, d3 i! `
如果A是向量,则prod(A)返回元素的乘积。  `5 e9 J4 _2 v2 A) t1 |* {  H

; [% }3 X) e4 s' v/ p如果A是非空矩阵,那么prod(A)将A的列视为向量并返回每列乘积的行向量。- l* i8 t4 {: {3 G5 i

4 o% T$ L! p" T2 X8 ?, f3 F1 `' v) f如果A是空的0乘0矩阵,则prod(A)返回1。+ `( R' _- a0 N& d
7 [1 k2 T8 `8 a% {! X
如果A是多维数组,那么prod(A)沿着第一个非单体维度行动并返回一个产品数组。 此尺寸的尺寸减小到1,而所有其他尺寸的尺寸保持不变。0 w9 u4 m) W, w. x5 s" u. ?
% R* R) B5 O$ l( w. W8 i

' J" E2 Q- {9 [. Z6 T% WFirst Nonsingleton Dimension' w( [1 X( Q8 z5 A, Y& e

3 L' q- N9 L" K2 l' X0 ^! N# t3 g' [6 o, z& _& q, W' \
The first nonsingleton dimension is the first dimension of an array whose size is not equal to 1.. F; v9 P9 J) r3 F( j. g/ x
( u7 w  e- v6 q" X
第一个非单体维度是数组的第一个维度,其大小不等于1。/ l  r8 C2 s+ B! z# U. t3 Y, {
- y) M5 ?0 g- g( a) J5 l. p
For example:! L# [  U3 N/ R- G
  ~/ I0 o4 X  z; x1 V  P
  • 如果X是1乘n的行向量,则第二个维度是X的第一个非单独维度。
  • 如果X是1乘0乘n的空数组,则第二维是X的第一个非单体维数。
  • 如果X是1乘1乘3的数组,则第三维是X的第一个非单体维数。
    * u& ?' e+ t& n& f* x: ?1 B
! t1 C& m! x% H8 [7 Z$ c

& B4 A6 t5 e) u% B当输入A为single类型时,prod计算并返回B为single。 对于所有其他数字和逻辑数据类型,prod计算并将B返回为double。
# d. M, `# T! |) B  `' B% C/ @' W/ N9 G# X: T) Y
B = prod(A,dim)沿维度dim返回产品。 例如,如果A是矩阵,则prod(A,2)是包含每行的乘积的列向量。% B& e$ r/ J1 G, D' L

* h( x! _! F+ ^) x6 iB = prod(___,type)使用前面语法中的任何输入参数返回类型指定的类中的数组。 type可以是'double','native'或'default'。
1 k+ @: X) @' r
7 J  I8 u3 s  w# r( |- {7 gB = prod(___,nanflag)指定是否在计算中包含或省略任何先前语法的NaN值.prod(A,'includenan')包括计算中的NaN值,而prod(A,'omitnan')忽略 他们。) w5 _2 T# L8 h" A
- f1 `3 P5 j0 P9 y; T! o
- k6 e$ |8 D) p8 x; W: P
Product of Elements in Each Column& ?1 T& v$ j9 E3 p

6 M6 j9 d# i  Y
2 L8 t$ f, f# u- Y! w3 uCreate a 3-by-3 array whose elements correspond to their linear indices.
! d2 _8 H. A4 X9 P* p- L. F# P7 \" M2 S: h
A=[1:3:7;2:3:8;3:3:9]
1 W' q3 R( i6 p! x# a9 X, }8 _$ c. u5 s2 y$ ^% r5 G7 y# o- P
A = 3×3* Q! f* l) [1 r! C2 f) l6 p  _2 |

/ u# Q) `+ ^! O     1     4     7: Y+ u. [, O+ n
     2     5     8
( I' ^6 M7 ^( e5 n3 J8 o6 w     3     6     99 |; a& I$ U7 B8 C2 d

+ T8 @& _2 E. X: q5 q+ |& Z+ W+ {6 L7 GFind 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).
9 e2 M3 S, R' j' F
, Z/ ?! U8 v& n! V( o+ X4 V1 b查找每列中元素的乘积。 第一个维度的长度为1,第二个维度的长度与 size(A,2)匹配。* J, u/ h$ R' W$ _
+ {: A) f' n4 }7 Y
B = prod(A)
! W2 O( ]/ y1 ]2 _" l' R# S7 F3 @% @, {$ j* n5 @
B = 1×36 [! U6 J9 B+ D0 T; l8 g( U9 C. _

' k3 [5 W- C+ d; u     6   120   504
1 r# s# K. V6 a, t/ e6 n2 [. o$ Q
- N4 b; ]% I9 V+ ]" ^) T3 Y
6 t. G: u0 c3 qProduct of Logical Input2 V: f8 ?3 v9 w" N5 \1 p4 H

1 _6 ~  L& W& a5 ?Create an array of logical values.% M* F6 G/ S+ @% j- a, g2 y$ L
8 ~- j3 n# d) E; t: j5 E* X
A = [true false; true true]% L, o) I/ F* d) p# ]

# u9 S3 A$ `/ Z" `" nA = 2x2 logical array  V; s. r& b* ]# L/ `  L" b

: }8 H' B0 B. |. B' G   1   0
' h9 V+ O( R+ o   1   1
3 P1 Q1 F; o! `( v9 \* q! P" J7 e# L
Find the product of the elements in each column.
+ Y: ?% M4 a) `# v2 y! M- v& u- R
  c; f, X/ X( j1 g  z7 V. FB = prod(A)
& z* {  Z4 O6 M4 ?. G8 U7 a  r  l" o5 J# g
B = 1×2& X7 w4 t+ m1 E0 o# R" o. i( h) ^
0 @5 t9 F+ n& I7 l) S& H
     1     00 _3 {0 x5 D$ d7 D+ j/ W% u% C
9 Q% Y3 ^# P' m: c+ F, V$ `
The output has type double.' `0 y% I+ X, \! i' F9 o( x
! W2 f9 p# E& J
class(B)' I9 o: V& U, \6 V7 n+ z3 @7 h" z

, d6 S  C4 u; t) M# R' ?0 r# ?ans = # @# |) N6 S4 g" v7 y7 K5 ]

7 o+ z/ U1 f4 w'double'
4 k* q4 X9 U0 C, `- }
; |) l8 E/ R0 j5 r/ `$ f
% }* b: R' |& q0 P! |Product of Elements in Each Row' q2 G5 E, i; x( s+ C

- A& F. M7 `0 _2 oCreate a 3-by-3 array whose elements correspond to their linear indices.% @* L: S! e5 X* ~* ?: X
  s( m; w) h8 e$ Z" n, _* T7 C* T
A=[1:3:7;2:3:8;3:3:9]
% ~$ y) R  v' ~" ?  m4 r1 U  g
: K1 A* O3 ?* A2 [A = 3×3& y8 U' I+ l9 \2 y, T* O

  F# m* {* I3 `7 T) A  ^( s- P, P. w     1     4     7
" G% b1 f6 |! K4 i8 o+ N' O9 o2 g4 T     2     5     8
. d0 \  X6 J& Q1 r     3     6     9( y7 G" ?# j: l
9 r3 e- M/ D' D2 m& J
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.: v/ J" k/ \6 j8 v7 ]
2 |$ _6 q  M3 F+ K* C) _- S, a) m% D
dim = 2;
- A, I6 G  C2 k2 E% \3 k. Q6 U" b9 K3 n, v
B = prod(A,dim)$ o$ P$ h" p6 N& L5 ?. ~4 k

$ z$ q% `  Z# R: v7 ^3 _B = 3×1. Y2 z0 d% ?3 ~1 r' U

. u8 z8 _9 {( k+ V9 z( N    288 `" Z6 n( B$ x) T) M: I. q
    800 l9 U) Y1 [) u0 G
   1625 o# s4 M5 k6 q( E
* M6 s! e* e! T, z% Q. i, }' @) Y

- D# l3 l, ]6 I5 g" bProduct of Elements in Each Plane
( x" }  z! _" c* M4 n5 D- ~' o- }+ ^, Q3 R% h3 H' ]
Create a 3-by-3-by-2 array whose elements correspond to their linear indices.
) F, Q! y# {' x$ E* _" R
4 i* c4 F& f3 v* N2 RA=[1:3:7;2:3:8;3:3:9];* \& A* J/ `9 K
, X3 p( _9 m3 k3 t$ u$ c
A(:,:,2)=[10:3:16;11:3:17;12:3:18]
' ?; v$ ~# p+ o) ^  Z' Q/ M8 c3 ~; M! a: a6 w
A = ( F& v) E3 x% ?8 v
A(:,:,1) =: W: P, V7 k4 y/ C3 }

) F2 Y) s, S% T8 {     1     4     75 ]/ ~9 w2 B3 r+ |- `
     2     5     8
( D6 y  ]. K, ~     3     6     91 ?3 l9 }( r3 j: f6 `; [  c+ S

4 @: ~, {, Y) e+ L9 s8 M' B( k
* s: M8 z' l8 x4 N! PA(:,:,2) =
$ x" ?  u3 k/ ~5 {: _7 y( @- s  S: {( k
    10    13    16- e9 z/ x. j, L( p
    11    14    178 c) i& N5 p9 L; F7 B( L& m' D
    12    15    18  A+ S# E8 ]% K7 ^  U
- X* D2 F, g) _7 T! E
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.
* [# S6 t) v! g  ^7 i0 B) c( ]% \* H) _- R! [
dim = 3;
5 r" ^* T! P9 {" a  GB = prod(A,dim)  A: @8 [! L3 l% b

1 P- y5 @; M, R6 W- Q* F4 k+ a8 wB = 3×3, s. A  U4 ~/ u" J

: e  q) N- G" H/ X, g# N, h    10    52   112
8 L) T% \# ?1 m$ y. _    22    70   136
: L% h/ j5 x5 y; j    36    90   162  ~5 ^$ N  V+ R

' B, l$ \. H7 X( \$ f2 i
: H) ]3 u% G& q4 q$ JSingle-Precision Input Treated as Double(单精度输入处理为双精度)
) G( N5 Z# `6 h( S
/ z! F9 W+ M$ L# O; f
7 _- o; s: ~' W: e% _& w/ U8 @! y; MCreate a 3-by-3 array of single-precision values.
  h( f' e& S2 W
2 t# m- b( K3 b3 C7 @; i/ i: BA = single([1200 1500 1800; 1300 1600 1900; 1400 1700 2000])
3 n. F& P$ C' f2 Q, u0 s( D; ?! Z
A = 3x3 single matrix2 ^2 F$ c- m9 f& D+ u) I0 L  Y- W

$ k) a4 ~6 _" C$ ]" K        1200        1500        18002 f. {6 O) [4 u% j5 J
        1300        1600        19003 I+ Q4 k( A% l9 {& c
        1400        1700        2000  S' V# r3 P: _/ V9 w, [$ \

2 G6 P# ]+ e( J/ P$ ~7 [Find the product of the elements in each row by multiplying in double precision.: C1 T9 Z+ l- Z( z! n, ]
* ^: w' j! F6 @; A
B = prod(A,2,'double')5 G# ^8 }) ^: X) M/ ?
3 [* \2 Q* L( T/ }
B = 3×1: ^( v4 Y: S! h5 C  M8 c
3 a6 b9 ^; n3 |0 U# [
109 ×$ Q) A! {% [* I( O- q4 N

7 v) _$ Z! V0 I! ~# l/ J    3.2400
3 u  j! S$ a- m' u  u    3.9520% U! Z- x0 u4 {# T
    4.76006 G! _/ ]  [' Y- S1 ^: P

& A0 m6 M! y+ V; f3 B% tThe output is double precision.
8 E+ ~- L! v0 H+ Q  K9 H3 t! T8 m" w% ], g& D+ v8 A
class(B)9 T7 Z) o8 \+ w1 _* v8 N
* R3 u' k" c, y5 [! V* t
ans = 9 {$ h/ t3 u3 p. O7 \, e
'double'
5 ~. @3 j4 T* e; S
( [9 ?3 `6 b( [& \( q& F8 R* b9 p# j8 i
Integer Data Type for Input and Output(输入和输出的整数数据类型)
$ g" \: ^7 \" J
! T$ O2 v: s9 e) _- i3 T! |4 I; aCreate a 3-by-3 array of 8-bit unsigned integers., W6 C5 {8 z6 t5 M- S, o% K
/ n3 Y. ^: E$ x' ^+ ^+ M
A = uint8([1:3:7;2:3:8;3:3:9])) A; H$ ?8 _4 |- R( l

# L; V. t0 R, |( ]A = 3x3 uint8 matrix
" C3 o* }9 s1 z- e2 g& M8 U& V! P- w: K/ H
   1   4   7
3 A0 ~  Z. H+ x& ]4 G. K7 g3 \/ Y   2   5   8( `" [: u. C; i' }* f9 s+ a0 ?
   3   6   9
% X7 x& [. _5 ~- u, h" \
: s- @9 s5 P3 s2 O* ?/ W1 t: KFind the product of the elements in each column natively in uint8.
9 K1 K# @/ b! f' ]# w9 N' A* U
6 P* n4 X7 z2 {: n) c9 ^4 c* s4 oB = prod(A,'native'); F/ o8 S9 J3 ~$ q: s
5 @$ r" ^5 J+ k- b: V
B = 1x3 uint8 row vector
* d4 d0 F9 n" ^7 ?2 Z; {; l% P8 f3 [$ l0 h
     6   120   255! G' p$ w) e& d2 g# B
2 T# H% U2 B9 L1 T
The result is an array of 8-bit unsigned integers.
5 o5 g. R2 z0 B0 P9 J; N
9 V  s; f1 \2 @9 t8 u. Lclass(B). P1 B0 v- x4 ]

1 w- \: h6 L; P* E; lans = % H' |4 R' j! t: P6 j
'uint8'
0 W- H) {; M6 U6 t- j' i6 c1 [
7 P0 i7 {" j: S3 [   X( ]$ H8 P) F& d2 b* Y
0 e' X! c' d/ e* m  A+ m& Z6 P
4 k' s. Q4 p9 B! ?# F" d9 L0 N
Product Excluding NaN
2 h" \- Z* E( s+ ?( s0 N: |, s" n# N/ V$ x% w
Create a vector and compute its product, excluding NaN values. If you do not specify 'omitnan', then prod(A)returns NaN.6 ?" z3 S" U$ {# B

0 b" J$ u6 R- s7 jA = [1 3 2 4 NaN 3 NaN 2];- T! f/ C4 u& I; e! u
, ]" X! _% o/ ?, T5 ]
P = prod(A,'omitnan')
9 k2 a# C6 }" Q$ s6 H1 R. N. h! p* ~$ m, x
P = 144! L- `5 q7 v/ n' Z7 @, D4 T) p9 X

1 X( R. ~' W8 N, h+ K$ a  f# ~0 {3 ]1 n& |& m/ p
您需要登录后才可以回帖 登录 | 注册

本版积分规则

关闭

推荐内容上一条 /1 下一条

EDA365公众号

关于我们|手机版|EDA365电子论坛网 ( 粤ICP备18020198号-1 )

GMT+8, 2025-6-11 11:54 , Processed in 0.078125 second(s), 26 queries , Gzip On.

深圳市墨知创新科技有限公司

地址:深圳市南山区科技生态园2栋A座805 电话:19926409050

快速回复 返回顶部 返回列表