MATLAB array manipulation tips and tricks Peter J. Acklam Emal: pack⊥ amon1i Url:http://home.onlineno/-pjacklam 1 8th October 2003
MATLAB array manipulation tips and tricks Peter J. Acklam E-mail: pjacklam@online.no URL: http://home.online.no/~pjacklam 18th October 2003
Contents 1 High-level ys low-level code L1 Introduction 1.2 Advantages and disadvantages 1.2. 1 Portability 1. 2.2 Verbosity 1.2.3 Speed 1. 2. 4 Obscurity 1.3 Words of warning 2 Operators, functions and special 2.1 Operators 222334 2.2 Built-in functions 2.3 M-file functions 3 Basic array properties 3.1 Siz 5666 3.1.1 Size along a specific dimension 3.1.2 Size along multiple dimen IsIons 3.2 Dimensions 3.2.1 Number of dimensions 3.3 Number of elements 7778 3.3.1 Empty arrays 4 Array indices and subseripts 5 Creating basic vectors, matrices and arrays 5.1Cr stant 5.1.1 When the class is determined by the scalar to replicate 5.1.2 When the class is stored in a string variable 5.2 Special vectors 000111 6 Shifting 6.1 Vectors 6.2 Matrices and arrays
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 7 Replicating elements and arrays 7.1 Creating a constant array 7.2 Replicating elements in vectors 13 7.2.1 Replicate each element a constant number of times 7.2.2 Replicate each element a variable number of times 13 7.3 Using KRON for replicating elements 73.1 KRoN with an matrix of ones 7.3.2 KRON with an identity matrix 8 Reshaping arrays 8.1 Subdividing 2D matrix 8.1.1 Create 4D array 8.1.2 Create 3D array (columns first) 8.1.3 Create 3D array (rows first) 66677 8.1.4 Create 2D matrix(columns firs St cOIumn ou 8.1.5 Create 2D matrix(columns first, row output) 8.1.6 Create 2D matrix(rows first, column output) 8.1.7 Create 2D matrix(rows first, row output) 78899 8.2 Stacking and unstacking pages Rotating matrices and arrays 9.1 Rotating 2D matrices 9.2 Rotating ND arrays 9.3 Rotating ND arrays around an arbitrary axis 9.4 Block-rotating 2D matrices 9 4.1"Inner" vs"outer block rotation 001223 9.4.2"Inner" block rotation 90 degrees counterclockwise 9. 4.3Inner' block rotation 180 degrees 94"Inner block rotation 90 degrees clockwise 9.4.5"Outer" block rotation 90 degrees counterclockwise 9.4.6"" block rotation 180 degrees 9. 4.7"Outer" block rotation 90 degrees clockwise 9.5 Blocktransposing a 2D matrix 9.5. 1"Inner" blocktransposing 9.5.2"Outer blocktransposing 10 Basic arithmetic operations ply arrays 10. 1. 1 Multiply each 2D slice with the same matrix(element-by-element) 10.1.2 Multiply each 2D slice with the same matrix(left) 10.1.3 Multiply each 2D slice with the same matrix(right) 10.1.4 Multiply matrix with every element of a vector 10.1.5 Multiply each 2D slice with corresponding element of a vector 10.1.6 Outer product of all rows in a matrix 10. 1.7 Keeping only diagonal elements of multiplication 32 0. 1.8 Products involving the Kronecker product 10.2 Divide arrays 10.2. 1 Divide each 2D slice with the same matrix(element-by-element) 10.2.2 Divide each 2D slice with the same matrix(left) 10.2. 3 Divide each 2D slice with the same matrix(right)
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 “Inner” block rotation 90 degrees counterclockwise . . . . . . . . . . . . . . 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 10 Basic arithmetic operations 30 10.1 Multiply arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30 10.1.1 Multiply each 2D slice with the same matrix (element-by-element) . . . . . . 30 10.1.2 Multiply each 2D slice with the same matrix (left) . . . . . . . . . . . . . . 30 10.1.3 Multiply each 2D slice with the same matrix (right) . . . . . . . . . . . . . . 30 10.1.4 Multiply matrix with every element of a vector . . . . . . . . . . . . . . . . 31 10.1.5 Multiply each 2D slice with corresponding element of a vector . . . . . . . . 32 10.1.6 Outer product of all rows in a matrix . . . . . . . . . . . . . . . . . . . . . . 32 10.1.7 Keeping only diagonal elements of multiplication . . . . . . . . . . . . . . . 32 10.1.8 Products involving the Kronecker product . . . . . . . . . . . . . . . . . . . 33 10.2 Divide arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33 10.2.1 Divide each 2D slice with the same matrix (element-by-element) . . . . . . . 33 10.2.2 Divide each 2D slice with the same matrix (left) . . . . . . . . . . . . . . . . 33 10.2.3 Divide each 2D slice with the same matrix (right) . . . . . . . . . . . . . . . 34
CONTENTS 11 More complicated arithmetic operations 11.1 Calculating distances 11.1.1 Euclidean distance 11.1.2 Distance between two points 11.1.3 Euclidean distance vector 11.14 Euclidean distance matrix 11.1.5 Special case when both matrices are identical 11.16 Mahalanobis distance 12 Statistics, probability and combinatorics 12. 1 Discrete uniform sampling with replacement 12.2 Discrete weighted sampling with replacement 12.3 Discrete uniform sampling without replacement 12.4 Combinations 12.4.1 Counting combination 12.4.2 Generating 12.5 Permutations 5.1 Counting permutations 12.5.2 Generating permutations 13 Identifying types of arrays 13.1 Numeric array 13.2 Real array 13.3 Identify real or purely imaginary elements 13.4 Array of negative, non-negative or positive values 13.5 Array of integers 13.6 Scalar 13. 7 Vector 13.8 Matrix 13.9 Array slice 14 Logical operators and comparisons 14.1 List of logical operators 14.2 Rules for logical operators 44 14.3 Quick tests before slow ones 15 Miscellaneous 15.1 Accessing elements on the dia 46 15.2 Creating index vector from index limits 47 15.3 Matrix with different incremental runs 15.4 Finding indices 15.4.1 First non-zero element in each column 15.4.2 First non-zero element in each row 15.4.3 Last non-zero element in each row 15.5 Run-length encoding and decoding 15.5.1 Run-length ene 5.6 Counting bi
CONTENTS iv 11 More complicated arithmetic operations 35 11.1 Calculating distances . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 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 12 Statistics, probability and combinatorics 38 12.1 Discrete uniform sampling with replacement . . . . . . . . . . . . . . . . . . . . . . 38 12.2 Discrete weighted sampling with replacement . . . . . . . . . . . . . . . . . . . . . 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 12.5 Permutations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39 12.5.1 Counting permutations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39 12.5.2 Generating permutations . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40 13 Identifying types of arrays 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 13.5 Array of integers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42 13.6 Scalar . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42 13.7 Vector . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43 13.8 Matrix . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43 13.9 Array slice . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43 14 Logical operators and comparisons 44 14.1 List of logical operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44 14.2 Rules for logical operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44 14.3 Quick tests before slow ones . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44 15 Miscellaneous 46 15.1 Accessing elements on the diagonal . . . . . . . . . . . . . . . . . . . . . . . . . . 46 15.2 Creating index vector from index limits . . . . . . . . . . . . . . . . . . . . . . . . 47 15.3 Matrix with different incremental runs . . . . . . . . . . . . . . . . . . . . . . . . . 48 15.4 Finding indices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 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 15.6 Counting bits . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51 Glossary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52 Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53
CONTENTS Appendix A MATLAB resourees
CONTENTS v Appendix . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55 A M A T L A B resources 55