MATLAB Array Manipulation Tips and Tricks
Total Page:16
File Type:pdf, Size:1020Kb
MATLAB array manipulation tips and tricks Peter J. Acklam E-mail: [email protected] URL: http://home.online.no/~pjacklam 14th August 2002 Abstract This document is intended to be a compilation of tips and tricks mainly related to efficient ways of performing low-level array manipulation in MATLAB. Here, “manipu- lation” means replicating and rotating arrays or parts of arrays, inserting, extracting, permuting and shifting elements, generating combinations and permutations of ele- ments, run-length encoding and decoding, multiplying and dividing arrays and calcu- lating distance matrics and so forth. A few other issues regarding how to write fast MATLAB code are also covered. I'd like to thank the following people (in alphabetical order) for their suggestions, spotting typos and other contributions they have made. Ken Doniger and Dr. Denis Gilbert Copyright © 2000–2002 Peter J. Acklam. All rights reserved. Any material in this document may be reproduced or duplicated for personal or educational use. MATLAB is a trademark of The MathWorks, Inc. (http://www.mathworks.com). TEX is a trademark of the American Mathematical Society (http://www.ams.org). Adobe PostScript and Adobe Acrobat Reader are trademarks of Adobe Systems Incorporated (http://www.adobe.com). The TEX source was written with the GNU Emacs text editor. The GNU Emacs home page is http://www.gnu.org/software/emacs/emacs.html. The TEX source was formatted with AMS-LATEX to produce a DVI (device independent) file. The PS (PostScript) version was created from the DVI file with dvips by Tomas Rokicki. The PDF (Portable Document Format) version was created from the PS file with ps2pdf, a part of Aladdin Ghostscript by Aladdin Enterprises. The PS and PDF version may be viewed and printed with software available at the Ghostscript, Ghostview and GSview Home Page, http://www.cs.wisc.edu/~ghost/index.html. The PDF version may also be viewed and printed with Adobe Acrobat Reader, which is available at http://www.adobe.com/products/acrobat/readstep.html. CONTENTS 2 Contents 1 Introduction 4 1.1 The motivation for writing this document . 4 1.2 Who this document is for . 5 1.3 Credit where credit is due . 5 1.4 Errors and feedback . 5 1.5 Vectorization . 5 2 Operators, functions and special characters 6 2.1 Operators . 6 2.2 Built-in functions . 7 2.3 M-file functions . 7 3 Basic array properties 8 3.1 Size . 8 3.1.1 Size along a specific dimension . 8 3.1.2 Size along multiple dimension . 8 3.2 Dimensions . 9 3.2.1 Number of dimensions . 9 3.2.2 Singleton dimensions . 9 3.3 Number of elements . 9 4 Array indices and subscripts 9 5 Creating vectors, matrices and arrays 9 5.1 Creating a constant array . 9 5.1.1 When the class is determined by the scalar to replicate . 9 5.1.2 When the class is stored in a string variable . 10 5.2 Special vectors . 11 5.2.1 Uniformly spaced elements . 11 6 Shifting 11 6.1 Vectors . 11 6.2 Matrices and arrays . 11 7 Replicating elements and arrays 12 7.1 Creating a constant array . 12 7.2 Replicating elements in vectors . 12 7.2.1 Replicate each element a constant number of times . 12 7.2.2 Replicate each element a variable number of times . 12 7.3 Using KRON for replicating elements . 12 7.3.1 KRON with an matrix of ones . 12 7.3.2 KRON with an identity matrix . 13 8 Reshaping arrays 13 8.1 Subdividing 2D matrix . 13 8.1.1 Create 4D array . 14 8.1.2 Create 3D array (columns first) . 14 8.1.3 Create 3D array (rows first) . 14 8.1.4 Create 2D matrix (columns first, column output) . 15 CONTENTS 3 8.1.5 Create 2D matrix (columns first, row output) . 15 8.1.6 Create 2D matrix (rows first, column output) . 16 8.1.7 Create 2D matrix (rows first, row output) . 16 8.2 Stacking and unstacking pages . 17 9 Rotating matrices and arrays 17 9.1 Rotating 2D matrices . 17 9.2 Rotating ND arrays . 17 9.3 Rotating ND arrays around an arbitrary axis . 18 9.4 Block-rotating 2D matrices . 19 9.4.1 “Inner” vs “outer” block rotation . 19 9.4.2 “Inner” block rotation 90 degrees counterclockwise . 20 9.4.3 “Inner” block rotation 180 degrees . 21 9.4.4 “Inner” block rotation 90 degrees clockwise . 22 9.4.5 “Outer” block rotation 90 degrees counterclockwise . 23 9.4.6 “Outer” block rotation 180 degrees . 24 9.4.7 “Outer” block rotation 90 degrees clockwise . 25 9.5 Blocktransposing a 2D matrix . 25 9.5.1 “Inner” blocktransposing . 25 9.5.2 “Outer” blocktransposing . 26 10 Multiply arrays 26 10.1 Multiply each 2D slice with the same matrix (element-by-element) . 26 10.2 Multiply each 2D slice with the same matrix (left) . 26 10.3 Multiply each 2D slice with the same matrix (right) . 26 10.4 Multiply matrix with every element of a vector . 27 10.5 Multiply each 2D slice with corresponding element of a vector . 28 10.6 Outer product of all rows in a matrix . 28 10.7 Keeping only diagonal elements of multiplication . 28 10.8 Products involving the Kronecker product . 29 11 Divide arrays 29 11.1 Divide each 2D slice with the same matrix (element-by-element) . 29 11.2 Divide each 2D slice with the same matrix (left) . 29 11.3 Divide each 2D slice with the same matrix (right) . 30 12 Calculating distances 30 12.1 Euclidean distance . 30 12.2 Distance between two points . 30 12.3 Euclidean distance vector . 31 12.4 Euclidean distance matrix . 31 12.5 Special case when both matrices are identical . 31 12.6 Mahalanobis distance . 32 13 Statistics, probability and combinatorics 33 13.1 Discrete uniform sampling with replacement . 33 13.2 Discrete weighted sampling with replacement . 33 13.3 Discrete uniform sampling without replacement . 33 13.4 Combinations . 33 13.4.1 Counting combinations . 34 1 INTRODUCTION 4 13.4.2 Generating combinations . 34 13.5 Permutations . 34 13.5.1 Counting permutations . 34 13.5.2 Generating permutations . 34 14 Types of arrays 35 14.1 Numeric array . 35 14.2 Real array . 35 14.3 Identify real or purely imaginary elements . 35 14.4 Array of negative, non-negative or positive values . 36 14.5 Array of integers . 36 14.6 Scalar . 36 14.7 Vector . 36 14.8 Matrix . 37 14.9 Array slice . 37 15 Logical operators and comparisons 37 15.1 List of logical operators . 37 15.2 Rules for logical operators . 37 15.3 Quick tests before slow ones . 38 16 Miscellaneous 38 16.1 Accessing elements on the diagonal . 38 16.2 Creating index vector from index limits . 39 16.3 Matrix with different incremental runs . 40 16.4 Finding indices . 41 16.4.1 First non-zero element in each column . 41 16.4.2 First non-zero element in each row . ..