MATLAB Programming
MATLAB Programming MATLAB Programming
2 Data Types A(B) ans = 5 15 30 35 50 In this case, the numeric values of array B designate the intended elements of A. Another type of array index, a logical index, designates the elements of A based on their position in the indexing array, B. Inthismasking type of operation, every true element in the indexing array is treated as a positional index into the array being accessed. Logical Indexing Example 1. This next example creates logical array B that satisfies the condition A > 0.5, and uses the positions of ones in B to index into A. Thisiscalledlogical indexing: A = rand(5); B = A > 0.5; A(B) = 0 A = 0.2920 0.3567 0.1133 0 0.0595 0 0.4983 0 0.2009 0.0890 0.3358 0.4344 0 0.2731 0.2713 0 0 0 0 0.4091 0.0534 0 0 0 0.4740 A simpler way to express this is A(A > 0.5) = 0 Logical Indexing Example 2. The next example highlights the location of the prime numbers in a magic square using logical indexing to set the nonprimes to 0: A = magic(4) A = 16 2 3 13 5 11 10 8 9 7 6 12 2-36
Logical Types 4 14 15 1 B = isprime(A) B = 0 1 1 1 1 1 0 0 0 1 0 0 0 0 0 0 A(~B) = 0; % Logical indexing A A = 0 2 3 13 5 11 0 0 0 7 0 0 0 0 0 0 Identifying Logical Arrays This table shows the commands you can use to determine whether or not an array x is logical. The last function listed, cellfun, operates on cell arrays, which you can read about in the section “Cell Arrays” on page 2-94. Command Operation whos(x) Display value and data type for x. islogical(x) isa(x, 'logical') class(x) cellfun('islogical', x) Return true if array is logical. Return true if array is logical. Return string with data type name. Check each cell array element for logical. 2-37
- 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
- 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 138 and 139: 2 Data Types Characters and Strings
- Page 140 and 141: 2 Data Types name = ['Thomas R. Lee
- Page 142 and 143: 2 Data Types Phoenix length(strings
- Page 144 and 145: 2 Data Types • “Setting Field W
- Page 146 and 147: 2 Data Types Ordered Sequentially s
- Page 148 and 149: 2 Data Types An alternate syntax, t
- Page 150 and 151: 2 Data Types Precision is not usual
- Page 152 and 153: 2 Data Types Note You can specify m
- Page 154 and 155: 2 Data Types operator, specifying t
- Page 156 and 157: 2 Data Types Valid Syntax Invalid S
- Page 158 and 159: 2 Data Types A = 'fate'; B = 'cake'
- Page 160 and 161: 2 Data Types while (any(remainder))
- Page 162 and 163: 2 Data Types Converting from String
- Page 164 and 165: 2 Data Types Function Summary MATLA
- Page 166 and 167: 2 Data Types Functions to Determine
- Page 168 and 169: 2 Data Types Date Strings There are
- Page 170 and 171: 2 Data Types • The day field is a
- Page 172 and 173: 2 Data Types datestr(d) ans = 01-Ma
- Page 174 and 175: 2 Data Types Utility Functions (Con
- Page 176 and 177: 2 Data Types • “Applying Functi
- Page 178 and 179: 2 Data Types where the arguments ar
- Page 180 and 181: 2 Data Types You can access subarra
- Page 182 and 183: 2 Data Types Dynamic Field Names Ex
- Page 184 and 185: 2 Data Types Deleting Fields from S
Logical Types<br />
4 14 15 1<br />
B = isprime(A)<br />
B =<br />
0 1 1 1<br />
1 1 0 0<br />
0 1 0 0<br />
0 0 0 0<br />
A(~B) = 0;<br />
% Logical indexing<br />
A<br />
A =<br />
0 2 3 13<br />
5 11 0 0<br />
0 7 0 0<br />
0 0 0 0<br />
Identifying Logical Arrays<br />
This table shows the commands you can use to determine whether or not an<br />
array x is logical. The last function listed, cellfun, operates on cell arrays,<br />
which you can read about in the section “Cell Arrays” on page 2-94.<br />
Command<br />
Operation<br />
whos(x) Display value and data type for x.<br />
islogical(x)<br />
isa(x, 'logical')<br />
class(x)<br />
cellfun('islogical', x)<br />
Return true if array is logical.<br />
Return true if array is logical.<br />
Return string with data type name.<br />
Check each cell array element for logical.<br />
2-37