23.06.2015 Views

MATLAB Programming

MATLAB Programming

MATLAB Programming

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Numeric Types<br />

• “Largest and Smallest Values for Integer Data Types” on page 2-9<br />

• “Warnings for Integer Data Types” on page 2-10<br />

• “Integer Functions” on page 2-13<br />

<strong>MATLAB</strong> supports 1-, 2-, 4-, and 8-byte storage for integer data. You can<br />

save memory and execution time for your programs if you use the smallest<br />

integer type that accommodates your data. For example, you don’t need a<br />

32-bit integer to store the value 100.<br />

Here are the eight integer data types, the range of values you can store with<br />

each type, and the <strong>MATLAB</strong> conversion function required to create that type:<br />

Data Type Range of Values Conversion Function<br />

Signed 8-bit integer -2 7 to 2 7 -1 int8<br />

Signed 16-bit integer -2 15 to 2 15 -1 int16<br />

Signed 32-bit integer -2 31 to 2 31 -1 int32<br />

Signed 64-bit integer -2 63 to 2 63 -1 int64<br />

Unsigned 8-bit integer 0to2 8 -1 uint8<br />

Unsigned 16-bit integer 0to2 16 -1 uint16<br />

Unsigned 32-bit integer 0to2 32 -1 uint32<br />

Unsigned 64-bit integer 0to2 64 -1 uint64<br />

Creating Integer Data<br />

<strong>MATLAB</strong> stores numeric data as double-precision floating point (double)<br />

by default. To store data as an integer, you need to convert from double to<br />

the desired integer type. Use one of the conversion functions shown in the<br />

table above.<br />

For example, to store 325 as a 16-bit signed integer assigned to variable x, type<br />

x = int16(325);<br />

If the number being converted to an integer has a fractional part, <strong>MATLAB</strong><br />

rounds to the nearest integer. If the fractional part is exactly 0.5, thenfrom<br />

2-7

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

Saved successfully!

Ooh no, something went wrong!