MATLAB Programming

MATLAB Programming MATLAB Programming

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

1 Data Structures 5000 50 B = [int8(50) int16(5000)] B = 50 127 The first operation returns a vector of 16-bit integers. The second returns a vector of 8-bit integers. The element int16(5000) is set to 127, the maximum value for an 8-bit signed integer. Thesamerulesapplytoverticalconcatenation: C = [int8(50); int16(5000)] C = 50 127 Note You can find the maximum or minimum values for any MATLAB integer type using the intmax and intmin functions. For floating-point types, use realmax and realmin. Example of Combining Signed with Unsigned. Now do the same exercise with signed and unsigned integers. Again, the left-most element determines thedatatypeforallelementsintheresultingmatrix: A = [int8(-100) uint8(100)] A = -100 100 B = [uint8(100) int8(-100)] B = 100 0 The element int8(-100) is set to zero because it is no longer signed. MATLAB evaluates each element prior to concatenating them into a combined array. In other words, the following statement evaluates to an 8-bit signed integer (equal to 50) and an 8-bit unsigned integer (unsigned -50 is set to 1-14

Creating and Concatenating Matrices zero) before the two elements are combined. Following the concatenation, the second element retains its zero value but takes on the unsigned int8 type: A = [int8(50), uint8(-50)] A = 50 0 Combining Integer and Noninteger Data If you combine integers with double, single, orlogical data types, all elements of the resulting matrix are given the data type of the left-most integer. For example, all elements of the following vector are set to int32: A = [true pi int32(1000000) single(17.32) uint8(250)] Empty Matrices If you construct a matrix using empty matrix elements, the empty matrices are ignored in the resulting matrix: A = [5.36; 7.01; []; 9.44] A = 5.3600 7.0100 9.4400 Concatenation Examples Herearesomeexamplesofdatatypeconversion during matrix construction. Combining Single and Double Types. Combining single values with double values yields a single matrix. Note that 5.73*10^300 is too big to be stored as a single, thus the conversion from double to single sets it to infinity. (The class function used in this example returns the data type for the input value): x = [single(4.5) single(-2.8) pi 5.73*10^300] x = 4.5000 -2.8000 3.1416 Inf 1-15

1 Data Structures<br />

5000 50<br />

B = [int8(50) int16(5000)]<br />

B =<br />

50 127<br />

The first operation returns a vector of 16-bit integers. The second returns a<br />

vector of 8-bit integers. The element int16(5000) is set to 127, the maximum<br />

value for an 8-bit signed integer.<br />

Thesamerulesapplytoverticalconcatenation:<br />

C = [int8(50); int16(5000)]<br />

C =<br />

50<br />

127<br />

Note You can find the maximum or minimum values for any <strong>MATLAB</strong><br />

integer type using the intmax and intmin functions. For floating-point types,<br />

use realmax and realmin.<br />

Example of Combining Signed with Unsigned. Now do the same exercise<br />

with signed and unsigned integers. Again, the left-most element determines<br />

thedatatypeforallelementsintheresultingmatrix:<br />

A = [int8(-100) uint8(100)]<br />

A =<br />

-100 100<br />

B = [uint8(100) int8(-100)]<br />

B =<br />

100 0<br />

The element int8(-100) is set to zero because it is no longer signed.<br />

<strong>MATLAB</strong> evaluates each element prior to concatenating them into a combined<br />

array. In other words, the following statement evaluates to an 8-bit signed<br />

integer (equal to 50) and an 8-bit unsigned integer (unsigned -50 is set to<br />

1-14

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

Saved successfully!

Ooh no, something went wrong!