MATLAB Programming
MATLAB Programming MATLAB Programming
1 Data Structures (shown as E below) within the brackets. Separate each element with a comma or space: row = [E 1 , E 2 , ..., E m ] row = [E 1 E 2 ... E m ] For example, to create a one row matrix of five elements, type A = [12 62 93 -8 22]; To start a new row, terminate the current row with a semicolon: A = [row 1 ; row 2 ; ...; row n ] This example constructs a 3 row, 5 column (or 3-by-5) matrix of numbers. Note that all rows must have the same number of elements: A = [12 62 93 -8 22; 16 2 87 43 91; -4 17 -72 95 6] A = 12 62 93 -8 22 16 2 87 43 91 -4 17 -72 95 6 The square brackets operator constructs two-dimensional matrices only, (including 0-by-0, 1-by-1, and 1-by-n matrices). To construct arrays of more than two dimensions, see “Creating Multidimensional Arrays” on page 1-54. For instructions on how to read or overwrite any matrix element, see “Matrix Indexing” on page 1-18. Entering Signed Numbers When entering signed numbers into a matrix, make sure that the sign immediately precedes the numeric value. Note that while the following two expressions are equivalent, 7 -2 +5 7 - 2 + 5 ans = ans = 10 10 the next two are not: 1-4
Creating and Concatenating Matrices [7 -2 +5] [7 - 2 + 5] ans = ans = 7 -2 5 10 Specialized Matrix Functions MATLAB has a number of functions that create different kinds of matrices. Some create specialized matrices like the Hankel or Vandermonde matrix. The functions shown in the table below create matrices for more general use. Function ones zeros eye accumarray diag magic rand randn randperm Description Create a matrix or array of all ones. Create a matrix or array of all zeros. Create a matrix with ones on the diagonal and zeros elsewhere. Distribute elements of an input matrix to specified locations in an output matrix, also allowing for accumulation. Create a diagonal matrix from a vector. Create a square matrix with rows, columns, and diagonals thatadduptothesamenumber. Create a matrix or array of uniformly distributed random numbers. Create a matrix or array of normally distributed random numbers and arrays. Create a vector (1-by-n matrix) containing a random permutation of the specified integers. Most of these functions return matrices of type double (double-precision floating point). However, you can easily build basic arrays of any numeric type using the ones, zeros, andeye functions. To do this, specify the MATLAB class name as the last argument: A = zeros(4, 6, 'uint32') A = 1-5
- Page 1 and 2: MATLAB ® 7 Programming
- Page 3: Revision History June 2004 First pr
- Page 6 and 7: Operating on Diagonal Matrices ....
- Page 8 and 9: Creating a Cell Array .............
- Page 10 and 11: Comma — , .......................
- Page 12 and 13: Examples of Anonymous Functions ...
- Page 14 and 15: Microsoft Excel Spreadsheets ......
- Page 16 and 17: Features of Object-Oriented Program
- Page 18 and 19: Object Precedence .................
- Page 20 and 21: 12 Programming Tips Command and Fun
- Page 22 and 23: Converting Between Strings and Cell
- Page 24 and 25: A External Interfaces Finding the D
- Page 26 and 27: 1 Data Structures Empty Matrices, S
- Page 30 and 31: 1 Data Structures 0 0 0 0 0 0 0 0 0
- Page 32 and 33: 1 Data Structures 0.6068 0.7621 0.8
- Page 34 and 35: 1 Data Structures 3 5 7 3 5 7 3 5 7
- Page 36 and 37: 1 Data Structures A = 3:0.2:3.8 A =
- Page 38 and 39: 1 Data Structures 5000 50 B = [int8
- Page 40 and 41: 1 Data Structures class(x) ans = si
- Page 42 and 43: 1 Data Structures Matrix Indexing T
- Page 44 and 45: 1 Data Structures A used in the pre
- Page 46 and 47: 1 Data Structures 16 5 9 4 . . . 12
- Page 48 and 49: 1 Data Structures Getting Informati
- Page 50 and 51: 1 Data Structures Function islogica
- Page 52 and 53: 1 Data Structures This example runs
- Page 54 and 55: 1 Data Structures A = 10 20 30 40 5
- Page 56 and 57: 1 Data Structures This changes matr
- Page 58 and 59: 1 Data Structures A = [1+9i 2-8i 3+
- Page 60 and 61: 1 Data Structures rp = rp + blocksi
- Page 62 and 63: 1 Data Structures A = [1:8; 11:18;
- Page 64 and 65: 1 Data Structures rand('state', 0);
- Page 66 and 67: 1 Data Structures OperatingonDiagon
- Page 68 and 69: 1 Data Structures Empty Matrices, S
- Page 70 and 71: 1 Data Structures These operations
- Page 72 and 73: 1 Data Structures Use the isscalar
- Page 74 and 75: 1 Data Structures Full and Sparse M
- Page 76 and 77: 1 Data Structures Multidimensional
1 Data Structures<br />
(shown as E below) within the brackets. Separate each element with a comma<br />
or space:<br />
row = [E 1<br />
, E 2<br />
, ..., E m<br />
] row = [E 1<br />
E 2<br />
... E m<br />
]<br />
For example, to create a one row matrix of five elements, type<br />
A = [12 62 93 -8 22];<br />
To start a new row, terminate the current row with a semicolon:<br />
A = [row 1<br />
; row 2<br />
; ...; row n<br />
]<br />
This example constructs a 3 row, 5 column (or 3-by-5) matrix of numbers.<br />
Note that all rows must have the same number of elements:<br />
A = [12 62 93 -8 22; 16 2 87 43 91; -4 17 -72 95 6]<br />
A =<br />
12 62 93 -8 22<br />
16 2 87 43 91<br />
-4 17 -72 95 6<br />
The square brackets operator constructs two-dimensional matrices only,<br />
(including 0-by-0, 1-by-1, and 1-by-n matrices). To construct arrays of more<br />
than two dimensions, see “Creating Multidimensional Arrays” on page 1-54.<br />
For instructions on how to read or overwrite any matrix element, see “Matrix<br />
Indexing” on page 1-18.<br />
Entering Signed Numbers<br />
When entering signed numbers into a matrix, make sure that the sign<br />
immediately precedes the numeric value. Note that while the following two<br />
expressions are equivalent,<br />
7 -2 +5 7 - 2 + 5<br />
ans = ans =<br />
10 10<br />
the next two are not:<br />
1-4