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 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

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

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

Saved successfully!

Ooh no, something went wrong!