MATLAB Programming
MATLAB Programming MATLAB Programming
2 Data Types the two equally nearby integers, MATLAB chooses the one for which the absolute value is larger in magnitude: x = 325.499; x = x + .001; int16(x) int16(x) ans = ans = 325 326 If you need to round a number using a rounding scheme other than the default, MATLAB provides four rounding functions: round, fix, floor, and ceil. Inthisexample,thefix function enables you to override the default and round towards zero when the fractional part of a number is .5: x = 325.5; int16(fix(x)) ans = 325 Arithmetic operations that involve both integers and floating-point always result in an integer data type. MATLAB rounds the result, when necessary, according to the default rounding algorithm. The example below yields an exact answer of 1426.75 which MATLAB then rounds to the next highest integer: int16(325) * 4.39 ans = 1427 The integer conversion functions are also useful when converting other data types, such as strings, to integers: str = 'Hello World'; int8(str) ans = 72 101 108 108 111 32 87 111 114 108 100 2-8
Numeric Types Arithmetic Operations on Integer Data Types MATLAB can perform integer arithmetic on the following types of data: • Integers or integer arrays of the same integer data type. This yields a result that has the same data type as the operands: x = uint32([132 347 528]) .* uint32(75); • Integers or integer arrays and scalar double-precision floating-point numbers. This yields a result that has the same data type as the integer operands: x = uint32([132 347 528]) .* 75.49; For all binary operations in which one operand is an array of integer data type and the other is a scalar double, MATLAB computes the operation using elementwise double-precision arithmetic, and then converts the result back to the original integer data type. For a list of the operations that support integer data types, see Nondouble Data Type Support in the arithmetic operators reference page. Largest and Smallest Values for Integer Data Types For each integer data type, there is a largest and smallest number that you can represent with that type. The table shownunder“Integers”onpage2-6 lists the largest and smallest values for each integer data type in the “Range of Values” column. You can also obtain these values with the intmax and intmin functions: intmax('int8') intmin('int8') ans = ans = 127 -128 If you convert a number that is larger than the maximum value of an integer data type to that type, MATLAB sets it to the maximum value. Similarly, if you convert a number that is smaller than the minimum value of the integer data type, MATLAB sets it to the minimum value. For example, x = int8(300) x = int8(-300) 2-9
- 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 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 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
- 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
2 Data Types<br />
the two equally nearby integers, <strong>MATLAB</strong> chooses the one for which the<br />
absolute value is larger in magnitude:<br />
x = 325.499; x = x + .001;<br />
int16(x)<br />
int16(x)<br />
ans = ans =<br />
325 326<br />
If you need to round a number using a rounding scheme other than the<br />
default, <strong>MATLAB</strong> provides four rounding functions: round, fix, floor, and<br />
ceil. Inthisexample,thefix function enables you to override the default<br />
and round towards zero when the fractional part of a number is .5:<br />
x = 325.5;<br />
int16(fix(x))<br />
ans =<br />
325<br />
Arithmetic operations that involve both integers and floating-point always<br />
result in an integer data type. <strong>MATLAB</strong> rounds the result, when necessary,<br />
according to the default rounding algorithm. The example below yields an<br />
exact answer of 1426.75 which <strong>MATLAB</strong> then rounds to the next highest<br />
integer:<br />
int16(325) * 4.39<br />
ans =<br />
1427<br />
The integer conversion functions are also useful when converting other data<br />
types, such as strings, to integers:<br />
str = 'Hello World';<br />
int8(str)<br />
ans =<br />
72 101 108 108 111 32 87 111 114 108 100<br />
2-8