MATLAB Programming

MATLAB Programming MATLAB Programming

cda.psych.uiuc.edu
from cda.psych.uiuc.edu More from this publisher
23.06.2015 Views

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

Resizing and Reshaping Matrices<br />

The preferred method for sizing an array that is expected to grow over time<br />

is to estimate the maximum possible size for the array, and preallocate this<br />

amount of memory for it at the time the array is created. In this way, your<br />

program performs one memory allocation that reserves one contiguous block.<br />

The following command preallocates enough space for a 25,000 by 10,000<br />

matrix, and initializes each element to zero:<br />

A = zeros(25000, 10000);<br />

Building a Preallocated Array<br />

Once memory has been preallocated for the maximum estimated size of the<br />

array, you can store your data in the array as you need it, each time appending<br />

to the existing data. This example preallocates a large array, and then reads<br />

blocks of data from a file into the array until it gets to the end of the file:<br />

blocksize = 5000;<br />

maxrows = 2500000; cols = 20;<br />

rp = 1; % row pointer<br />

% Preallocate A to its maximum possible size<br />

A = zeros(maxrows, cols);<br />

% Open the data file, saving the file pointer.<br />

fid = fopen('statfile.dat', 'r');<br />

while true<br />

% Read from file into a cell array. Stop at EOF.<br />

block = textscan(fid, '%n', blocksize*cols);<br />

if isempty(block{1}) break, end;<br />

% Convert cell array to matrix, reshape, place into A.<br />

A(rp:rp+blocksize-1, 1:cols) = ...<br />

reshape(cell2mat(block), blocksize, cols);<br />

% Process the data in A.<br />

evaluate_stats(A);<br />

% User-defined function<br />

% Update row pointer<br />

1-35

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!