_MATLAB+array+manipulation+tips+and+tricks+%28ang%29.pdf

(265 KB) Pobierz
M ATLAB array manipulation tips and tricks
Peter J. Acklam
18th October 2003
Copyright © 2000–2003 Peter J. Acklam. All rights reserved.
Any material in this document may be reproduced or duplicated for personal or educational use.
M ATLAB is a trademark of The MathWorks, Inc.
T E X is a trademark of the American Mathematical Society.
Adobe, Acrobat, Acrobat Reader, and PostScript are trademarks of Adobe Systems Incorporated.
Contents
Preface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
v
1
High-level vs low-level code
1
1.1
Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
1
1.2
Advantages and disadvantages
. . . . . . . . . . . . . . . . . . . . . . . . . . . . .
1
1.2.1
Portability . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
1
1.2.2
Verbosity
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
2
1.2.3
Speed . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
2
1.2.4
Obscurity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
2
1.2.5
Difficulty
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
2
1.3
Words of warning . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
2
2
Operators, functions and special characters
3
2.1
Operators
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
3
2.2
Built-in functions
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
4
2.3
M-file functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
5
3
Basic array properties
6
3.1
Size
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
6
3.1.1
Size along a specific dimension
. . . . . . . . . . . . . . . . . . . . . . . .
6
3.1.2
Size along multiple dimensions
. . . . . . . . . . . . . . . . . . . . . . . .
6
3.2
Dimensions
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
7
3.2.1
Number of dimensions . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
7
3.2.2
Singleton dimensions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
7
3.3
Number of elements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
7
3.3.1
Empty arrays
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
8
4
Array indices and subscripts
9
5
Creating basic vectors, matrices and arrays
10
5.1
Creating a constant array
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
10
5.1.1
When the class is determined by the scalar to replicate
. . . . . . . . . . . .
10
5.1.2
When the class is stored in a string variable
. . . . . . . . . . . . . . . . . .
11
5.2
Special vectors
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
11
5.2.1
Uniformly spaced elements . . . . . . . . . . . . . . . . . . . . . . . . . . .
11
6
Shifting
12
6.1
Vectors
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
12
6.2
Matrices and arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
12
ii
CONTENTS
iii
7
Replicating elements and arrays
13
7.1
Creating a constant array
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
13
7.2
Replicating elements in vectors . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
13
7.2.1
Replicate each element a constant number of times
. . . . . . . . . . . . . .
13
7.2.2
Replicate each element a variable number of times
. . . . . . . . . . . . . .
13
7.3
Using KRON for replicating elements
. . . . . . . . . . . . . . . . . . . . . . . . .
14
7.3.1
KRON with an matrix of ones
. . . . . . . . . . . . . . . . . . . . . . . . .
14
7.3.2
KRON with an identity matrix . . . . . . . . . . . . . . . . . . . . . . . . .
14
8
Reshaping arrays
16
8.1
Subdividing 2D matrix
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
16
8.1.1
Create 4D array . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
16
8.1.2
Create 3D array (columns first) . . . . . . . . . . . . . . . . . . . . . . . . .
16
8.1.3
Create 3D array (rows first)
. . . . . . . . . . . . . . . . . . . . . . . . . .
17
8.1.4
Create 2D matrix (columns first, column output)
. . . . . . . . . . . . . . .
17
8.1.5
Create 2D matrix (columns first, row output)
. . . . . . . . . . . . . . . . .
18
8.1.6
Create 2D matrix (rows first, column output)
. . . . . . . . . . . . . . . . .
18
8.1.7
Create 2D matrix (rows first, row output)
. . . . . . . . . . . . . . . . . . .
19
8.2
Stacking and unstacking pages
. . . . . . . . . . . . . . . . . . . . . . . . . . . . .
19
9
Rotating matrices and arrays
20
9.1
Rotating 2D matrices
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
20
9.2
Rotating ND arrays
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
20
9.3
Rotating ND arrays around an arbitrary axis
. . . . . . . . . . . . . . . . . . . . . .
21
9.4
Block-rotating 2D matrices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
22
9.4.1
“Inner” vs “outer” block rotation . . . . . . . . . . . . . . . . . . . . . . . .
22
9.4.2
23
9.4.3
“Inner” block rotation 180 degrees . . . . . . . . . . . . . . . . . . . . . . .
24
9.4.4
“Inner” block rotation 90 degrees clockwise . . . . . . . . . . . . . . . . . .
25
9.4.5
“Outer” block rotation 90 degrees counterclockwise
. . . . . . . . . . . . .
26
9.4.6
“Outer” block rotation 180 degrees
. . . . . . . . . . . . . . . . . . . . . .
27
9.4.7
“Outer” block rotation 90 degrees clockwise
. . . . . . . . . . . . . . . . .
28
9.5
Blocktransposing a 2D matrix
. . . . . . . . . . . . . . . . . . . . . . . . . . . . .
28
9.5.1
“Inner” blocktransposing . . . . . . . . . . . . . . . . . . . . . . . . . . . .
28
9.5.2
“Outer” blocktransposing . . . . . . . . . . . . . . . . . . . . . . . . . . . .
29
30
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
30
10.1.1
30
10.1.2
Multiply each 2D slice with the same matrix (left)
. . . . . . . . . . . . . .
30
10.1.3
30
10.1.4
Multiply matrix with every element of a vector
. . . . . . . . . . . . . . . .
31
10.1.5
32
10.1.6
Outer product of all rows in a matrix . . . . . . . . . . . . . . . . . . . . . .
32
10.1.7
32
10.1.8
Products involving the Kronecker product . . . . . . . . . . . . . . . . . . .
33
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
33
10.2.1
33
10.2.2
Divide each 2D slice with the same matrix (left) . . . . . . . . . . . . . . . .
33
10.2.3
34
CONTENTS
iv
35
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
35
11.1.1
Euclidean distance
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
35
11.1.2
Distance between two points . . . . . . . . . . . . . . . . . . . . . . . . . .
35
11.1.3
Euclidean distance vector
. . . . . . . . . . . . . . . . . . . . . . . . . . .
35
11.1.4
Euclidean distance matrix
. . . . . . . . . . . . . . . . . . . . . . . . . . .
35
11.1.5
Special case when both matrices are identical
. . . . . . . . . . . . . . . . .
36
11.1.6
Mahalanobis distance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
36
38
12.1 Discrete uniform sampling with replacement . . . . . . . . . . . . . . . . . . . . . .
38
. . . . . . . . . . . . . . . . . . . . .
38
12.3 Discrete uniform sampling without replacement . . . . . . . . . . . . . . . . . . . .
38
12.4 Combinations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
39
12.4.1
Counting combinations . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
39
12.4.2
Generating combinations . . . . . . . . . . . . . . . . . . . . . . . . . . . .
39
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
39
12.5.1
Counting permutations . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
39
12.5.2
Generating permutations . . . . . . . . . . . . . . . . . . . . . . . . . . . .
40
41
13.1 Numeric array . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
41
13.2 Real array . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
41
13.3 Identify real or purely imaginary elements . . . . . . . . . . . . . . . . . . . . . . .
42
13.4 Array of negative, non-negative or positive values . . . . . . . . . . . . . . . . . . .
42
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
42
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
42
13.7 Vector . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
43
13.8 Matrix . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
43
13.9 Array slice . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
43
44
14.1 List of logical operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
44
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
44
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
44
46
. . . . . . . . . . . . . . . . . . . . . . . . . .
46
. . . . . . . . . . . . . . . . . . . . . . . .
47
15.3 Matrix with different incremental runs . . . . . . . . . . . . . . . . . . . . . . . . .
48
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
48
15.4.1
First non-zero element in each column . . . . . . . . . . . . . . . . . . . . .
48
15.4.2
First non-zero element in each row . . . . . . . . . . . . . . . . . . . . . . .
49
15.4.3
Last non-zero element in each row . . . . . . . . . . . . . . . . . . . . . . .
50
15.5 Run-length encoding and decoding . . . . . . . . . . . . . . . . . . . . . . . . . . .
50
15.5.1
Run-length encoding . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
50
15.5.2
Run-length decoding . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
51
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
51
Glossary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
52
Index
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
53
Zgłoś jeśli naruszono regulamin