MATLAB Programming
MATLAB Programming MATLAB Programming
1 Data Structures A = [1+9i 2-8i 3+7i; 4-6i 5+5i 6-4i] A = 1.0000 + 9.0000i 2.0000 -8.0000i 3.0000 + 7.0000i 4.0000 -6.0000i 5.0000 + 5.0000i 6.0000 -4.0000i B = A' B = 1.0000 -9.0000i 4.0000 + 6.0000i 2.0000 + 8.0000i 5.0000 -5.0000i 3.0000 -7.0000i 6.0000 + 4.0000i Rotating a Matrix. Rotate the matrix by 90 degrees: B = rot90(A) B = 10 11 12 7 8 9 4 5 6 1 2 3 Flipping a Matrix. Flip A in a left-to-right direction: B = fliplr(A) B = 10 7 4 1 11 8 5 2 12 9 6 3 Preallocating Memory Repeatedly expanding the size of an array over time, (for example, adding more elements to it each time through a programming loop), can adversely affect the performance of your program. This is because • MATLAB has to spend time allocating more memory each time you increase thesizeofthearray. • This newly allocated memory is likely to be noncontiguous, thus slowing down any operations that MATLAB needs to perform on the array. 1-34
Resizing and Reshaping Matrices The preferred method for sizing an array that is expected to grow over time is to estimate the maximum possible size for the array, and preallocate this amount of memory for it at the time the array is created. In this way, your program performs one memory allocation that reserves one contiguous block. The following command preallocates enough space for a 25,000 by 10,000 matrix, and initializes each element to zero: A = zeros(25000, 10000); Building a Preallocated Array Once memory has been preallocated for the maximum estimated size of the array, you can store your data in the array as you need it, each time appending to the existing data. This example preallocates a large array, and then reads blocks of data from a file into the array until it gets to the end of the file: blocksize = 5000; maxrows = 2500000; cols = 20; rp = 1; % row pointer % Preallocate A to its maximum possible size A = zeros(maxrows, cols); % Open the data file, saving the file pointer. fid = fopen('statfile.dat', 'r'); while true % Read from file into a cell array. Stop at EOF. block = textscan(fid, '%n', blocksize*cols); if isempty(block{1}) break, end; % Convert cell array to matrix, reshape, place into A. A(rp:rp+blocksize-1, 1:cols) = ... reshape(cell2mat(block), blocksize, cols); % Process the data in A. evaluate_stats(A); % User-defined function % Update row pointer 1-35
- 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 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 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
- Page 102 and 103: 2 Data Types Cell Arrays (p. 2-94)
- Page 104 and 105: 2 Data Types Data Type Summary The
- Page 106 and 107: 2 Data Types Numeric Types Numeric
1 Data Structures<br />
A = [1+9i 2-8i 3+7i; 4-6i 5+5i 6-4i]<br />
A =<br />
1.0000 + 9.0000i 2.0000 -8.0000i 3.0000 + 7.0000i<br />
4.0000 -6.0000i 5.0000 + 5.0000i 6.0000 -4.0000i<br />
B = A'<br />
B =<br />
1.0000 -9.0000i 4.0000 + 6.0000i<br />
2.0000 + 8.0000i 5.0000 -5.0000i<br />
3.0000 -7.0000i 6.0000 + 4.0000i<br />
Rotating a Matrix. Rotate the matrix by 90 degrees:<br />
B = rot90(A)<br />
B =<br />
10 11 12<br />
7 8 9<br />
4 5 6<br />
1 2 3<br />
Flipping a Matrix. Flip A in a left-to-right direction:<br />
B = fliplr(A)<br />
B =<br />
10 7 4 1<br />
11 8 5 2<br />
12 9 6 3<br />
Preallocating Memory<br />
Repeatedly expanding the size of an array over time, (for example, adding<br />
more elements to it each time through a programming loop), can adversely<br />
affect the performance of your program. This is because<br />
• <strong>MATLAB</strong> has to spend time allocating more memory each time you increase<br />
thesizeofthearray.<br />
• This newly allocated memory is likely to be noncontiguous, thus slowing<br />
down any operations that <strong>MATLAB</strong> needs to perform on the array.<br />
1-34