MATLAB Programming
MATLAB Programming MATLAB Programming
1 Data Structures Computing with Multidimensional Arrays Many of the MATLAB computational and mathematical functions accept multidimensional arrays as arguments. These functions operate on specific dimensions of multidimensional arrays; that is, they operate on individual elements, on vectors, or on matrices. Operating on Vectors Functions that operate on vectors, like sum, mean, and so on, by default typically work on the first nonsingleton dimension of a multidimensional array. Most of these functions optionally let you specify a particular dimension on which to operate. There are exceptions, however. For example, the cross function, which finds the cross product of two vectors, works on the first nonsingleton dimension having length 3. Note In many cases, these functions have other restrictions on the input arguments — for example, some functions that accept multiple arrays require that the arrays be the same size. Refer to the online help for details on function arguments. Operating Element-by-Element MATLAB functions that operate element-by-element on two-dimensional arrays, like the trigonometric and exponential functions in the elfun directory, work in exactly the same way for multidimensional cases. For example, the sin function returns an array the same size as the function’s input argument. Each element of the output array is the sine of the corresponding element of the input array. Similarly, the arithmetic, logical, and relational operators all work with corresponding elements of multidimensional arrays that are the same size in every dimension. If one operand is a scalar and one an array, the operator applies the scalar to each element of the array. Operating on Planes and Matrices Functions that operate on planes or matrices, such as the linear algebra and matrix functions in the matfun directory, do not accept multidimensional 1-66
Multidimensional Arrays arrays as arguments. That is, you cannot use the functions in the matfun directory, or the array operators *, ^, \, or/, with multidimensional arguments. Supplying multidimensional arguments or operands in these cases results in an error. You can use indexing to apply a matrix function or operator to matrices within a multidimensional array. For example, create a three-dimensional array A: A = cat(3, [1 2 3; 9 8 7; 4 6 5], [0 3 2; 8 8 4; 5 3 5], ... [6 4 7; 6 8 5; 5 4 3]); Applying the eig function to the entire multidimensional array results in an error: eig(A) ??? Error using ==> eig Input arguments must be 2-D. You can, however, apply eig to planes within the array. For example, use colon notation to index just one page (in this case, the second) of the array: eig(A(:,:,2)) ans = 12.9129 -2.6260 2.7131 Note In the first case, subscripts are not colons; you must use squeeze to avoid an error. For example, eig(A(2,:,:)) results in an error because thesizeoftheinputis[1 3 3]. The expression eig(squeeze(A(2,:,:))), however, passes a valid two-dimensional matrix to eig. Organizing Data in Multidimensional Arrays You can use multidimensional arrays to represent data in two ways: • As planes or pages of two-dimensional data. You can then treat these pages as matrices. 1-67
- 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 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 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
- Page 108 and 109: 2 Data Types the two equally nearby
- Page 110 and 111: 2 Data Types x = x = 127 -128 Also,
- Page 112 and 113: 2 Data Types Set the warning state
- Page 114 and 115: 2 Data Types Floating-Point Numbers
- Page 116 and 117: 2 Data Types isfloat(x) ans = 1 You
- Page 118 and 119: 2 Data Types In this example, 7.5 d
- Page 120 and 121: 2 Data Types 8.881784197001252e-016
- Page 122 and 123: 2 Data Types Note that the order of
- Page 124 and 125: 2 Data Types Complex Numbers Comple
- Page 126 and 127: 2 Data Types NaN MATLAB represents
- Page 128 and 129: 2 Data Types All available formats
- Page 130 and 131: 2 Data Types Integer Functions Func
- Page 132 and 133: 2 Data Types Infinity and NaN Funct
- Page 134 and 135: 2 Data Types 1 0 0 1 0 1 1 0 The MA
- Page 136 and 137: 2 Data Types A(B) ans = 5 15 30 35
- Page 138 and 139: 2 Data Types Characters and Strings
Multidimensional Arrays<br />
arrays as arguments. That is, you cannot use the functions in the matfun<br />
directory, or the array operators *, ^, \, or/, with multidimensional<br />
arguments. Supplying multidimensional arguments or operands in these<br />
cases results in an error.<br />
You can use indexing to apply a matrix function or operator to matrices within<br />
a multidimensional array. For example, create a three-dimensional array A:<br />
A = cat(3, [1 2 3; 9 8 7; 4 6 5], [0 3 2; 8 8 4; 5 3 5], ...<br />
[6 4 7; 6 8 5; 5 4 3]);<br />
Applying the eig function to the entire multidimensional array results in<br />
an error:<br />
eig(A)<br />
??? Error using ==> eig<br />
Input arguments must be 2-D.<br />
You can, however, apply eig to planes within the array. For example, use<br />
colon notation to index just one page (in this case, the second) of the array:<br />
eig(A(:,:,2))<br />
ans =<br />
12.9129<br />
-2.6260<br />
2.7131<br />
Note In the first case, subscripts are not colons; you must use squeeze to<br />
avoid an error. For example, eig(A(2,:,:)) results in an error because<br />
thesizeoftheinputis[1 3 3]. The expression eig(squeeze(A(2,:,:))),<br />
however, passes a valid two-dimensional matrix to eig.<br />
Organizing Data in Multidimensional Arrays<br />
You can use multidimensional arrays to represent data in two ways:<br />
• As planes or pages of two-dimensional data. You can then treat these pages<br />
as matrices.<br />
1-67