1 / 16

Advanced Topic- Data Types

Advanced Topic- Data Types. Introduction to MATLAB 7 Engineering 161. Data Types- Chapter 10. To take advantage of more of MATLAB’s capabilities we need to learn about data types.

kyoko
Télécharger la présentation

Advanced Topic- Data Types

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Advanced Topic- Data Types Introduction to MATLAB 7 Engineering 161

  2. Data Types- Chapter 10 • To take advantage of more of MATLAB’s capabilities we need to learn about data types. • Up to this point, our assignments have largely used numerical data types, namely x = 5 assigns a double precision floating point representation of the number 5 to x. This is MATLAB’s default case. • In addition to floating point representations, MATLAB supports single precision floating point, signed and unsigned integer data types, character and string data types, complex numbers and logical data types.

  3. Data Types- Introduction II • Prior to MATLAB 7 some of these data types were undefined or required conversion from one type to another during the calculations. • MATLAB 7 extended the data types and now has most operations on these data types being performed natively without explicit conversions. • Let’s look at the data types now supported in MATLAB 7

  4. Data Types- Introduction III • Data types supported in MATLAB 7 • Signed and unsigned integers of 8, 16, 32, and 64-bit lengths. • Single and double precision floating point numbers, or 32 or 64-bit representations. • Complex numbers • Character and character string data types. • Relational and logical data types. • Symbolic data types We have some familiarity with floating point representations, character strings and logical data types. Let’s look at data types in more detail.

  5. Data Types- Introduction IV • Why care about data types? It really depends on what you want to do, that is, the kinds of calculations or manipulations that you want to perform. Often integer arithmetic is sufficient and it runs faster than double precision arithmetic, or if you want to manipulate strings of characters in a program, you need to know how to define character strings and manipulate them.

  6. Numerical Data Types- Real Numbers • The MATLAB default is double-precision floating point representation for numeric data. MATLAB uses 64 bits to represent these numbers. • Single precision floating point numbers may be appropriate in some cases, here MATLAB use 32 bits to represent numbers. Single precision will improve run time over double precision representations. • Use the functions single and double to convert back and forth between the two representations.

  7. Numerical Data Types- Integers • Integers can be stored in a variety of formats, 8, 16, 32 and 64 bits, both as unsigned or signed. Unsigned integers can be only positive numbers. The more bits used the larger the range of integers that can be represented. In general, you can’t mix operations on signed and unsigned integers, so be careful. Use the int32 and uint32 functions to specify signed and unsigned 32 bit integers., for example. x = int32(10); the number 10 would be assigned to x and stored as signed integer using 32 bits in 2’s complement form

  8. Numerical Data Types- Complex Numbers • Calculations using complex numbers are as easy to perform in MATLAB as calculations using real numbers. Complex numbers are often used in engineering applications particularly in electrical engineering. Use i and j (reserved MATLAB symbols) in the specification of complex numbers, consider z = 5 + 3*i where 5 would be the real part and 3 the imaginary part of the complex number. Use the operations +, -, / and * to manipulate complex numbers.

  9. Numerical Data Types- Complex Numbers • So for example, v = 5 + 3*i; % the * is optional in the z = 2 – i*1; % imaginary part, I like to use it. v*z % recall that i2 = -1 v*z = 12 + i

  10. Character and String Data • MATLAB can store character strings, for example H = ‘Holly’; % this is a character string H becomes a character array where each character in the string, including spaces, is a separate element in a character array. H(5) would be the character y. MATLAB uses ASCII to represent characters, which is an 8 bit coding scheme.

  11. Symbolic Data • We’ve already seen how to represent symbolic data, used either the sym or the syms functions to create symbolic expressions. L = sym(‘x^2 – 2’); or syms x; L = x^2 -2;

  12. Logical Data • We’ve also had some familiarity with logical expressions and logical data types; x= 5, y = -3; x > y ans = 1

  13. Multi-dimensional Arrays • So far we have only dealt with 1 and 2-dimensional arrays, A = [1,2,3; 4,5,6] would be an example • But MATLAB can deal with multidimensional arrays as well, B(:, :, 1) would be the first page of a 2-dimensional array, B(:, :, 2) would be the second page, etc. • Element B(2, 3, 2) would be the element located in the second row, third column on page two of the array called B. • Check out practice exercise 10.3 on page 354.

  14. Character Arrays • Previously we saw how to create character variables, H = ‘Holly’; • Consider the column array names = [‘Holly’; ‘Sam’; ‘Judy’]; This would generate an error message as MATLAB requires all the rows to have the same number of characters, clearly violated here.

  15. Character Arrays II One way to get around this is to pad the strings so they have the same length, names = [‘Holly’; ‘ Sam’; ‘ Judy’]; or use the char function names = char(‘Holly’, ‘Sam’, ‘Judy’); produces a column array of the names. Don’t forget the num2str function if you want to combine character strings and numbers into the same array. Different types rarely mix in MATLAB.

  16. Cell Arrays • Last but not least is the cell array. We have seen that for some structures, data type matters and different data types can not be mixed in arrays in general. To fix this limitation, cell arrays have come on the scene. • Cell arrays permit elements to be of different data types. They would typically be used in database applications where you might want to mix character strings with numerical data.

More Related