Using MATLAB (779505), страница 4
Текст из файла (страница 4)
. . . . . . . . . .Information About Nonzero Elements . . . . . . . . . . . . . . . . . .Viewing Sparse Matrices Graphically . . . . . . . . . . . . . . . . . .The find Function and Sparse Matrices . . . . . . . . . . . . . . . . .16-1316-1316-1516-16Example: Adjacency Matrices and Graphs . . . . . . . . . .
. . .Introduction to Adjacency Matrices . . . . . . . . . . . . . . . . . . . .Graphing Using Adjacency Matrices . . . . . . . . . . . . . . . . . . .The Bucky Ball . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .An Airflow Model . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . .16-1716-1716-1816-1816-23Sparse Matrix Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . .Computational Considerations . . . . . . . . . . . . . . . . . . . . . . . .Standard Mathematical Operations . . . . . . . . . . . . . .
. . . . . .Permutation and Reordering . . . . . . . . . . . . . . . . . . . . . . . . . .Factorization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Simultaneous Linear Equations . . . . . . . . . . . . . . . . . . . . . . .Eigenvalues and Singular Values . . . . . . . . . . . . . . . .
. . . . . .16-2516-2516-2516-2616-3016-3616-39Selected Bibliography . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16-42xiiiProgramming and Data TypesM-File Programming17MATLAB Programming: A Quick Start . . . . . . . . . . . . . . . . .Kinds of M-Files . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . .What’s in an M-File? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Providing Help for Your Programs . . . . . . . . . . . . . . . . . . . . . .Creating M-Files: Accessing Text Editors . . . . . . . . . . . . . . . .17-317-317-417-417-5Scripts . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17-7Simple Script Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17-7Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . 17-8Simple Function Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17-8Basic Parts of a Function M-File . . . . . . . . . . . . . . . . . . . . . . . . 17-9Function Names . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . 17-11How Functions Work . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17-12Checking the Number of Function Arguments . . . . . . . . . . . 17-14Passing Variable Numbers of Arguments . . . . . . .
. . . . . . . . 17-16Local and Global Variables . . . . . . . . . . . . . . . . . . . . . . . . . . 17-19Persistent Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17-20Special Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17-21Data Types . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . 17-22Keywords . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17-25Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Arithmetic Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Relational Operators . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Logical Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Operator Precedence . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .xiv Contents17-2617-2617-2817-2917-32Flow Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . .if, else, and elseif . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .switch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .while . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. .for . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .continue . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .break . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . .try ... catch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .return . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .17-3517-3517-3717-3917-4017-4117-4117-4217-42Subfunctions . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . 17-43Private Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17-45Subscripting and Indexing . . . . . . . . . . . . . . . . . . . . . . . . . . . 17-46Subscripting . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . 17-46Advanced Indexing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17-51String Evaluation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17-54eval . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17-54feval . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17-54Command/Function Duality . . . . . . . . . . . . . . . . . . . . . . . . . . 17-56Empty Matrices . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17-57Operating on an Empty Matrix . . . . . . . . . . . . . . . . . . . . . . . . 17-57Using Empty Matrices with If or While . . . . . . . . . . . . . . . . . 17-58Errors and Warnings . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17-59Error Handling with eval and lasterr . . . . . . . . . . . . . . . . . . . 17-59Displaying Error and Warning Messages . . . . . . . . . . . . . . . . 17-60Dates and Times . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17-62Date Formats . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . 17-62Current Date and Time . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17-67Obtaining User Input . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17-69Prompting for Keyboard Input . . . . . . . . . . . . . . .
. . . . . . . . . 17-69Pausing During Execution . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17-69xvShell Escape Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17-70Optimizing MATLAB Code . . . . . . . . . . . . . .
. . . . . . . . . . . . .Vectorizing Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Preallocating Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Making Efficient Use of Memory . . . . . . . . .
. . . . . . . . . . . . .17-7117-7117-7317-74Character Arrays (Strings)18Character Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Creating Character Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . .Creating Two-Dimensional Character Arrays . . . .
. . . . . . . . .Converting Characters to Numeric Values . . . . . . . . . . . . . . . .18-518-518-618-7Cell Arrays of Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18-8Converting to a Cell Array of Strings . . . . . . . . . . .
. . . . . . . . . 18-8String Comparisons . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Comparing Strings For Equality . . . . . . . . . . . . . . . . . . . . . . .Comparing for Equality Using Operators . . . . . . . . . . . . . .
. .Categorizing Characters Within a String . . . . . . . . . . . . . . . .18-1018-1018-1118-12Searching and Replacing . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18-13String/Numeric Conversion . . . . . . . . . . . . . . . . . . . . . . . . . . 18-15Array/String Conversion . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . 18-16Multidimensional Arrays19Multidimensional Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19-3Creating Multidimensional Arrays . . . . . . . . . . . . . . . . . . . . . . 19-4Accessing Multidimensional Array Properties . . . . .
. . . . . . . . 19-8xvi ContentsIndexing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19-9Reshaping . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19-10Permuting Array Dimensions . . . . . . . . . . .
. . . . . . . . . . . . . . 19-12Computing with Multidimensional Arrays . . . . . . . . . . . . .Operating on Vectors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Operating Element-by-Element . . . . . . . . . . . . . . . .
. . . . . . .Operating on Planes and Matrices . . . . . . . . . . . . . . . . . . . . .19-1419-1419-1419-15Organizing Data in Multidimensional Arrays . . . . . . . . . . 19-16Multidimensional Cell Arrays . . . . . . . . . . . . . . . . . . . . . . . . 19-18Multidimensional Structure Arrays . . . . . . . . . . . . . . . . . . . 19-19Applying Functions to Multidimensional Structure Arrays . 19-20Structures and Cell Arrays20Structures . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20-4Building Structure Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20-5Accessing Data in Structure Arrays . . . . . . . . . . . . . . . . . . . . . 20-7Finding the size of Structure Arrays . . . . . . .
. . . . . . . . . . . . 20-10Adding Fields to Structures . . . . . . . . . . . . . . . . . . . . . . . . . . . 20-10Deleting Fields from Structures . . . . . . . . . . . . . . . . . . . . . . . 20-10Applying Functions and Operators . . . . . . . . . . . . . . . . . . . . .















