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

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

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

Saved successfully!

Ooh no, something went wrong!