8 n! N0 \' `, L4 }Description- {5 J8 R' [7 B; j _2 O' T
; p8 I5 S$ V! u5 t8 H! G
B = prod(A) returns the product of the array elements of A.; }0 S* c6 W7 B2 Y, g
/ U6 b0 X1 j/ c" E$ a6 E9 N
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.: T9 z$ t6 H/ |' [' E4 Q8 O1 P' ?
# g7 s9 @6 W: F% A* ]/ {
B = prod(A) 返回A的数组元素的乘积。' e2 K# }& n$ b1 b3 G
7 ?/ _) c) B- _. L! N
如果A是向量,则prod(A)返回元素的乘积。 " H! N5 F: n; S n7 M9 {& F4 f0 R 2 ?2 \% c- {! ~& i# d如果A是非空矩阵,那么prod(A)将A的列视为向量并返回每列乘积的行向量。 , v) O, N- N* j6 v, G $ N% ~ t% L2 J8 b如果A是空的0乘0矩阵,则prod(A)返回1。0 t3 h9 H% {5 A. Y# b/ X, l
9 Y9 x/ N- n3 {& Z% C; dProduct of Elements in Each Column) G* O6 y9 g/ t$ g
4 l1 c. \5 m4 O% S1 N
0 B, ~' {8 K9 s3 a) r
Create a 3-by-3 array whose elements correspond to their linear indices. 6 [# j/ C6 @) f2 X" \- p$ S * \. M) T4 Y. b) XA=[1:3:7;2:3:8;3:3:9] 1 Q: S' c% r4 G8 a: e 5 S f1 R8 u. W: RA = 3×3 ! _7 N0 U0 ]8 {3 U S( @ 1 w9 L1 | [. U3 X6 Z 1 4 7 ; v" |8 @& r: P6 C0 N# I 2 5 8 , Z0 a4 n+ N! G4 O1 W3 v$ E0 S' }8 R" c8 h 3 6 9 6 T7 N- f& r7 X% G) d1 x7 d; l 9 T; `) l8 }1 G: ]& f8 A0 D: L6 a7 B& AFind 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 i: M& a) H7 P6 Q' G/ ]! Y4 Y. L6 i1 ? j T
查找每列中元素的乘积。 第一个维度的长度为1,第二个维度的长度与 size(A,2)匹配。 4 I7 p7 u8 x0 k* s9 t& {3 Z* v `4 L
B = prod(A) # @: h: w0 Q3 l6 n. X% f . n) o2 M$ P* z9 r% iB = 1×3 J7 V1 F) N% { O0 r % `3 `" M9 a4 ~7 r: w U8 N0 h$ Z 6 120 504 2 X% }5 y) P( v# y7 z+ A. D' @1 t2 ~* I% f A
9 P) E; P% [$ K( L5 @* RProduct of Logical Input " \ @: x9 J% o$ G% L1 G7 X2 @ T+ I7 r$ }
Create an array of logical values. 4 y0 W v% d7 Q1 V J ; \% Y! e0 g$ D5 P: E% K& kA = [true false; true true]4 }, U3 ^; j) N- g6 J
2 ?7 e# e, j. B4 P$ Q/ o4 }
A = 2x2 logical array; M. m u) ~5 |) Y- G! Y0 L
9 V5 `9 Y' Q+ l3 i5 }* Z( b 1 05 n/ u7 a9 Q& O v! T$ w
1 10 q2 N5 M' _% K) S( h0 G
; h0 H8 j/ J- S/ g) U }, m& N9 UFind the product of the elements in each column.2 z0 G% h. c0 \( t/ w+ S
4 {4 n8 ]. a) U- zB = prod(A)$ S+ Q' {6 t1 ]8 [0 V
5 j; _! k) u0 m
B = 1×2# o9 E# P/ |9 f9 y0 V {2 F1 w
, D' |+ p6 K* M# D+ S- m
1 0 " Q5 ~3 w& T& h& A6 y2 | * p' C- a( {6 i( a4 q5 h; xThe output has type double.9 S8 V! R) I7 C3 \8 w( J
4 s: H4 N3 W+ |& B. xclass(B) 7 r0 O/ z \3 U- I( k- I) L5 B5 ]4 s( `( ~- d3 d, Y5 M* `
ans = 2 ~. d! Y2 C. q6 f8 P. l; ?3 M: w' M6 e' Q/ Q& c! r& ^" M
'double'+ \1 H7 ^, u2 A' L
8 \. M2 p# b8 J5 @6 j ) H- K2 Q* s1 g) AProduct of Elements in Each Row : P/ N) J" H1 q! p/ }0 h 2 D; O& F: ~2 ?" U- T% b+ J8 S
Create a 3-by-3 array whose elements correspond to their linear indices.( ~9 _" e" F0 n/ M8 P( o
& h! a: W9 b2 [5 [5 Q) EA = 3×3 ( r8 t) ^4 _' e/ X$ ^+ v. b* _" N9 O$ b6 Y) _
1 4 7 . v. M) E; S+ a/ f: ]. Q7 c 2 5 8: S) E: N7 l% l, J
3 6 91 t! T- x6 C6 w9 K! O
, r& ^$ O) D: 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.+ K; l$ }6 B( M2 w2 q$ E. Q4 Y
w4 ^* N, C) L. |/ }4 k4 y! Wdim = 2;; K9 P; Z! ^# l" B" p; J
( Q% H2 A+ ]1 P# D; j' DB = prod(A,dim) ~) Y( j, f9 U 9 ?: j- {/ [5 CB = 3×11 p4 J6 H8 G, R2 ]
" P* V9 Y( {0 t; O) m: G/ g
28 , v& X& g( H( |7 F" @! b 80& u: J- G6 r, r" ~3 c" q
162 " M$ V' D7 P B7 H) r3 {4 {) s4 F2 Z) [
: ?; _* j( X" Z+ o; C Z. A Product of Elements in Each Plane6 R) O) Y; ~- p. c# |2 t* r l
7 @3 j9 ^; w& ]: F) V* CCreate a 3-by-3-by-2 array whose elements correspond to their linear indices. 8 H; `( H8 @% G3 d! A8 q$ t/ p# a2 i. t# N$ [2 Q( T3 A
A=[1:3:7;2:3:8;3:3:9];& J O. ^* W B C8 o; [9 f
: v" h* B( M5 h/ V; O0 B
A(:,:,2)=[10:3:16;11:3:17;12:3:18] 6 k8 }: H, Q) V8 K $ _9 ~0 R; Q% t* o% l& EA = ' v; _" N% D* O2 W+ T% [
A(:,:,1) =0 n) K, W* f' V+ c- R- I8 q1 y
( ?# ~+ I$ \0 [; |1 S' p% W. b% F 1 4 75 o' m1 v0 m5 P$ U
2 5 8 # [" ?5 m5 j- e0 t; a& ^ 3 6 9 9 o2 c0 c% [) P: n$ U- X7 R" I9 g4 v+ h ?$ s( `; g
3 C* G! u- J+ S) EA(:,:,2) =# n) O5 f3 f3 `) R6 B' u- l
0 O, c* ^5 O0 B) A; u
10 13 16 ; V$ ^. {& u8 w% N2 v 11 14 17- `3 d3 {3 f6 M8 Q
12 15 18$ y4 Y. C: j k' ~: B& j
! g8 q. j* ^) o0 s: h
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. 1 I8 }4 W( s: P: @! F+ @, B0 q) l* L0 b- c- R
dim = 3; . V* w- A, V2 D# V# s) t. b# y6 yB = prod(A,dim) 5 H2 V* c8 {' e* | G2 y2 E * Y; |+ ` C# Z- ~( a% x. kB = 3×3 ' B7 C3 _" Z/ T$ m% e/ d( G! I0 C+ b6 L8 H5 ~
10 52 1126 O: h; r9 I% m
22 70 136 v& j% K5 [& f1 e
36 90 162 E# X( f" u! l8 e1 { u 8 H5 c6 D3 |, j8 H4 q 4 J% @' a# K t* ^; D8 B0 XSingle-Precision Input Treated as Double(单精度输入处理为双精度) 8 M% M( X( Q: J" L% @6 G % R k a6 x. f' {4 B
; z P. b2 B) u+ ~Create a 3-by-3 array of single-precision values.* W3 U4 o% w8 H- `+ a
. _/ t8 l2 I" r8 n+ G- gA = single([1200 1500 1800; 1300 1600 1900; 1400 1700 2000]) - {% f' n( E# [$ @ - v2 M/ L5 E' S8 |" GA = 3x3 single matrix 6 ~' r: q% N+ `5 R# W( D& U' S ! g* H7 y& t; @5 m: \" n. g 1200 1500 18006 ^* p! F _6 x! h
1300 1600 1900 3 V7 w( e7 q6 O; y 1400 1700 2000 & r b/ g& ?4 w* S+ V, c$ H U8 R9 k* m% b3 N
Find the product of the elements in each row by multiplying in double precision. * b* H. w0 @; q+ @# y; i1 s ) |5 U9 m" Q1 I! WB = prod(A,2,'double') ! s% R+ r! }$ J5 G# g6 f 1 h1 ^0 O! ` [' `* ?( T. mB = 3×1 ( H) U% c) g' Y4 \' s0 ]) ? 8 q& V8 }! B( P' i) V109 ×0 B! Z/ z( s, f3 i- H
( Z, u- Z1 y5 G 3.2400 ) Y) G3 @2 b6 l 3.9520 7 g' j' q1 X, f" q* l' D 4.7600 . t, K) }: ] i( |8 U! P + J( K+ j4 n j* O& d6 |The output is double precision. b' S: }, S# k$ z
. L4 }5 i/ W! J- P
class(B) ; t/ r/ b$ J7 j+ B/ z$ p # M# h) J; D9 o; t1 n# Qans = , {/ H5 | V) z- I2 o, D
'double' 5 C2 u/ a/ z8 v8 p+ n' h 7 C9 K4 Y S0 m7 R 8 |' l: y/ ]# y+ ]Integer Data Type for Input and Output(输入和输出的整数数据类型) 4 g+ _! w8 ~" V) P2 y5 K0 a6 t6 g ; b5 A7 {3 X h+ kCreate a 3-by-3 array of 8-bit unsigned integers. 6 U7 }' P( _+ p8 V6 S% X) N$ L: t
A = uint8([1:3:7;2:3:8;3:3:9])4 o! I9 Z! |" c9 b! o
" @/ R9 {' W3 e5 D/ s) O
A = 3x3 uint8 matrix: n8 {9 m8 x3 H3 y
+ ], i( ]' l; k3 L. O! L 1 4 77 r# k2 R' i- m. u( |9 G; Q
2 5 8 # a4 |* j+ f6 c j. } 3 6 9 $ Z" Z7 r( g9 e6 E$ O K+ ` 8 g) V% p( V/ x9 Y, U: ZFind the product of the elements in each column natively in uint8. $ V0 z" _9 T4 f ; t/ j1 s z0 \B = prod(A,'native') - f! l. S8 f+ R, W% E / s& @% {- `+ I6 ?B = 1x3 uint8 row vector & W/ J5 o4 p% G) l ( z1 K- ~; J" z# n 6 120 255, o3 q, v: ? i
% k: K+ A, K: rThe result is an array of 8-bit unsigned integers.0 ~1 Z4 c4 l x" q
7 x$ D" y1 ~" _
class(B)" \+ e; s! l7 |
4 J+ x4 R, Z* [$ d1 G! \4 q3 Q% k' Q
ans = 6 B6 w0 F/ e4 k
'uint8'6 K* p1 r5 a' r8 ^
% d& U$ R% w) A- t2 R: X