MATLAB Programming
MATLAB Programming MATLAB Programming
1 Data Structures Function islogical isnumeric isreal isstruct Description Determine if input is a logical array. Determine if input is a numeric array. Determine if input is an array of real numbers. Determine if input is a MATLAB structure array. Example Using isnumeric and isreal Pick out the real numeric elements from this vector: A = [5+7i 8/7 4.23 39j pi 9-2i]; for m = 1:numel(A) if isnumeric(A(m)) && isreal(A(m)) disp(A(m)) end end The values returned are 1.1429 4.2300 3.1416 Data Structures Used in the Matrix These functions test elements of a matrix for a specific data structure. Function isempty isscalar issparse isvector Description Determine if input has any dimension with size zero. Determine if input is a 1-by-1 matrix. Determine if input is a sparse matrix. Determine if input is a 1-by-n or n-by-1 matrix. 1-26
Resizing and Reshaping Matrices Resizing and Reshaping Matrices You can easily enlarge or shrink the size of a matrix, modify its shape, or rotate it about various axes. This section covers • “Expanding the Size of a Matrix” on page 1-27 • “Diminishing the Size of a Matrix” on page 1-31 • “Reshaping a Matrix” on page 1-32 • “Preallocating Memory” on page 1-34 Expanding the Size of a Matrix You can expand the size of any existing matrix as long as doing so does not give the resulting matrix an irregular shape. (See “Keeping Matrices Rectangular” on page 1-8). For example, you can vertically combine a 4-by-3 matrix and 7-by-3 matrix because all rows of the resulting matrix have the same number of columns (3). Twowaysofexpandingthesizeofanexistingmatrixare • Concatenating new elements onto the matrix • Storing to a location outside the bounds of the matrix Note If you intend to expand the size of a matrix repeatedly over time as it requires more room (usually done in a programming loop), it is advisable to preallocate space for the matrix when you initially create it. See “Preallocating Memory” on page 1-34. Concatenating Onto the Matrix Concatenation is most useful when you want to expand a matrix by adding newelementsorblocksthatarecompatible in size with the original matrix. This means that the size of all matrices being joined along a specific dimension must be equal along that dimension. See “Concatenating Matrices” on page 1-7. 1-27
- 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 28 and 29: 1 Data Structures (shown as E below
- 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 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
- Page 78 and 79: 1 Data Structures As you add dimens
- Page 80 and 81: 1 Data Structures Generating Arrays
- Page 82 and 83: 1 Data Structures • size — Retu
- Page 84 and 85: 1 Data Structures For example, cons
- Page 86 and 87: 1 Data Structures Avoiding Ambiguit
- Page 88 and 89: 1 Data Structures Permuting Array D
- Page 90 and 91: 1 Data Structures Computing with Mu
- Page 92 and 93: 1 Data Structures • As multivaria
- Page 94 and 95: 1 Data Structures A{2,2} = 7; B{1,1
- Page 96 and 97: 1 Data Structures Summary of Matrix
- Page 98 and 99: 1 Data Structures Functions to Dete
1 Data Structures<br />
Function<br />
islogical<br />
isnumeric<br />
isreal<br />
isstruct<br />
Description<br />
Determine if input is a logical array.<br />
Determine if input is a numeric array.<br />
Determine if input is an array of real numbers.<br />
Determine if input is a <strong>MATLAB</strong> structure array.<br />
Example Using isnumeric and isreal<br />
Pick out the real numeric elements from this vector:<br />
A = [5+7i 8/7 4.23 39j pi 9-2i];<br />
for m = 1:numel(A)<br />
if isnumeric(A(m)) && isreal(A(m))<br />
disp(A(m))<br />
end<br />
end<br />
The values returned are<br />
1.1429<br />
4.2300<br />
3.1416<br />
Data Structures Used in the Matrix<br />
These functions test elements of a matrix for a specific data structure.<br />
Function<br />
isempty<br />
isscalar<br />
issparse<br />
isvector<br />
Description<br />
Determine if input has any dimension with size zero.<br />
Determine if input is a 1-by-1 matrix.<br />
Determine if input is a sparse matrix.<br />
Determine if input is a 1-by-n or n-by-1 matrix.<br />
1-26