MATLAB Programming

MATLAB Programming MATLAB Programming

cda.psych.uiuc.edu
from cda.psych.uiuc.edu More from this publisher
23.06.2015 Views

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

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

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

Saved successfully!

Ooh no, something went wrong!