MATLAB Programming
MATLAB Programming MATLAB Programming
1 Data Structures This example runs a user-defined function compareResults on the data in matrices stats04 and stats03. Each time through the loop, it concatenates the results of this function onto the end of the data stored in comp04: col = 10; comp04 = []; for k = 1:50 t = compareResults(stats04(k,1:col), stats03(k,1:col)); comp04 = [comp04; t]; end Concatenating to a Structure or Cell Array. Youcanaddontoarraysof structures or cells in the same way as you do with ordinary matrices. This example creates a 3-by-8 matrix of structures S, eachhaving3fields:x, y, and z, and then concatenates a second structure matrix S2 onto the original: Create a 3-by-8 structure array S: for k = 1:24 S(k) = struct('x', 10*k, 'y', 10*k+1, 'z', 10*k+2); end S = reshape(S, 3, 8); Create a second array that is 3-by-2 and uses the same field names: for k = 25:30 S2(k-24) = struct('x', 10*k, 'y', 10*k+1, 'z', 10*k+2); end S2= reshape(S2, 3, 2); Concatenate S2 onto S along the horizontal dimension: S = [S S2] S = 3x10 struct array with fields: x y z 1-28
Resizing and Reshaping Matrices Adding Smaller Blocks to a Matrix To add one or more elements to a matrix where the sizes are not compatible, you can often just store the new elements outside the boundaries of the original matrix. MATLAB automatically pads the matrix with zeros to keep it rectangular. Constructa3-by-5matrix,andattempttoaddanewelementtoitusing concatenation. The operation fails because you are attempting to join a one-column matrix with one that has five columns: A = [ 10 20 30 40 50; ... 60 70 80 90 100; ... 110 120 130 140 150]; A = [A; 160] ??? Error using ==> vertcat All rows in the bracketed expression must have the same number of columns. Try this again, but this time do it in such a way that enables MATLAB to make adjustments to the size of the matrix. Store the new element in row 4, a row that does not yet exist in this matrix. MATLAB expands matrix A by an entire new row by padding columns 2 through 5 with zeros: A(4,1) = 160 A = 10 20 30 40 50 60 70 80 90 100 110 120 130 140 150 160 0 0 0 0 Note Attempting to read from nonexistent matrix locations generates an error. You can only write to these locations. You can also expand the matrix by adding a matrix instead of just a single element: A(4:6,1:3) = magic(3)+100 1-29
- 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 50 and 51: 1 Data Structures Function islogica
- 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
- Page 100 and 101: 1 Data Structures 1-76
Resizing and Reshaping Matrices<br />
Adding Smaller Blocks to a Matrix<br />
To add one or more elements to a matrix where the sizes are not compatible,<br />
you can often just store the new elements outside the boundaries of the<br />
original matrix. <strong>MATLAB</strong> automatically pads the matrix with zeros to keep it<br />
rectangular.<br />
Constructa3-by-5matrix,andattempttoaddanewelementtoitusing<br />
concatenation. The operation fails because you are attempting to join a<br />
one-column matrix with one that has five columns:<br />
A = [ 10 20 30 40 50; ...<br />
60 70 80 90 100; ...<br />
110 120 130 140 150];<br />
A = [A; 160]<br />
??? Error using ==> vertcat<br />
All rows in the bracketed expression must have the same<br />
number of columns.<br />
Try this again, but this time do it in such a way that enables <strong>MATLAB</strong> to<br />
make adjustments to the size of the matrix. Store the new element in row 4, a<br />
row that does not yet exist in this matrix. <strong>MATLAB</strong> expands matrix A by an<br />
entire new row by padding columns 2 through 5 with zeros:<br />
A(4,1) = 160<br />
A =<br />
10 20 30 40 50<br />
60 70 80 90 100<br />
110 120 130 140 150<br />
160 0 0 0 0<br />
Note Attempting to read from nonexistent matrix locations generates an<br />
error. You can only write to these locations.<br />
You can also expand the matrix by adding a matrix instead of just a single<br />
element:<br />
A(4:6,1:3) = magic(3)+100<br />
1-29