MATLAB Programming
MATLAB Programming MATLAB Programming
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
- Page 1 and 2: MATLAB ® 7 Programming
- Page 3: Revision History June 2004 First pr
- Page 6 and 7: Operating on Diagonal Matrices ....
- Page 8 and 9: Creating a Cell Array .............
- Page 10 and 11: Comma — , .......................
- Page 12 and 13: Examples of Anonymous Functions ...
- Page 14 and 15: Microsoft Excel Spreadsheets ......
- Page 16 and 17: Features of Object-Oriented Program
- Page 18 and 19: Object Precedence .................
- Page 20 and 21: 12 Programming Tips Command and Fun
- Page 22 and 23: Converting Between Strings and Cell
- Page 24 and 25: A External Interfaces Finding the D
- Page 26 and 27: 1 Data Structures Empty Matrices, S
- Page 28 and 29: 1 Data Structures (shown as E below
- Page 30 and 31: 1 Data Structures 0 0 0 0 0 0 0 0 0
- Page 32 and 33: 1 Data Structures 0.6068 0.7621 0.8
- Page 34 and 35: 1 Data Structures 3 5 7 3 5 7 3 5 7
- Page 36 and 37: 1 Data Structures A = 3:0.2:3.8 A =
- Page 40 and 41: 1 Data Structures class(x) ans = si
- Page 42 and 43: 1 Data Structures Matrix Indexing T
- Page 44 and 45: 1 Data Structures A used in the pre
- Page 46 and 47: 1 Data Structures 16 5 9 4 . . . 12
- Page 48 and 49: 1 Data Structures Getting Informati
- Page 50 and 51: 1 Data Structures Function islogica
- Page 52 and 53: 1 Data Structures This example runs
- Page 54 and 55: 1 Data Structures A = 10 20 30 40 5
- Page 56 and 57: 1 Data Structures This changes matr
- 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
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