MATLAB Programming
MATLAB Programming MATLAB Programming
1 Data Structures As you add dimensions to an array, you also add subscripts. A four-dimensional array, for example, has four subscripts. The first two reference a row-column pair; the second two access the third and fourth dimensions of data. Note The general multidimensional array functions reside in the datatypes directory. Creating Multidimensional Arrays You can use the same techniques to create multidimensional arrays that you use for two-dimensional matrices. In addition, MATLAB provides a special concatenation function that is useful for building multidimensional arrays. This section discusses • “Generating Arrays Using Indexing” on page 1-54 • “Extending Multidimensional Arrays” on page 1-55 • “Generating Arrays Using MATLAB Functions” on page 1-56 • “Building Multidimensional Arrays with the cat Function” on page 1-56 Generating Arrays Using Indexing One way to create a multidimensional array is to create a two-dimensional array and extend it. For example, begin with a simple two-dimensional array A. A = [5 7 8; 0 1 9; 4 3 6]; A is a 3-by-3 array, that is, its row dimension is 3 and its column dimension is 3. To add a third dimension to A, A(:,:,2) = [1 0 4; 3 5 6; 9 8 7] MATLAB responds with A(:,:,1) = 5 7 8 0 1 9 1-54
Multidimensional Arrays 4 3 6 A(:,:,2) = 1 0 4 3 5 6 9 8 7 You can continue to add rows, columns, or pages to the array using similar assignment statements. Extending Multidimensional Arrays To extend A in any dimension: • Increment or add the appropriate subscript and assign the desired values. • Assign the same number of elements to corresponding array dimensions. For numeric arrays, all rows must have the same number of elements, all pages must have the same number of rows and columns, and so on. You can take advantage of the MATLAB scalar expansion capabilities, together with the colon operator, to fill an entire dimension with a single value: A(:,:,3) = 5; A(:,:,3) ans = 5 5 5 5 5 5 5 5 5 To turn A into a 3-by-3-by-3-by-2, four-dimensional array, enter A(:,:,1,2) = [1 2 3; 4 5 6; 7 8 9]; A(:,:,2,2) = [9 8 7; 6 5 4; 3 2 1]; A(:,:,3,2) = [1 0 1; 1 1 0; 0 1 1]; Note that after the first two assignments MATLAB pads A with zeros, as needed, to maintain the corresponding sizes of dimensions. 1-55
- 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 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 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
- 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
Multidimensional Arrays<br />
4 3 6<br />
A(:,:,2) =<br />
1 0 4<br />
3 5 6<br />
9 8 7<br />
You can continue to add rows, columns, or pages to the array using similar<br />
assignment statements.<br />
Extending Multidimensional Arrays<br />
To extend A in any dimension:<br />
• Increment or add the appropriate subscript and assign the desired values.<br />
• Assign the same number of elements to corresponding array dimensions.<br />
For numeric arrays, all rows must have the same number of elements, all<br />
pages must have the same number of rows and columns, and so on.<br />
You can take advantage of the <strong>MATLAB</strong> scalar expansion capabilities,<br />
together with the colon operator, to fill an entire dimension with a single value:<br />
A(:,:,3) = 5;<br />
A(:,:,3)<br />
ans =<br />
5 5 5<br />
5 5 5<br />
5 5 5<br />
To turn A into a 3-by-3-by-3-by-2, four-dimensional array, enter<br />
A(:,:,1,2) = [1 2 3; 4 5 6; 7 8 9];<br />
A(:,:,2,2) = [9 8 7; 6 5 4; 3 2 1];<br />
A(:,:,3,2) = [1 0 1; 1 1 0; 0 1 1];<br />
Note that after the first two assignments <strong>MATLAB</strong> pads A with zeros, as<br />
needed, to maintain the corresponding sizes of dimensions.<br />
1-55