1 / 90

CMPSC 201

CMPSC 201. Other Kinds of Arrays. Kinds of Data Stored in MATLAB Matrices. Symbolic Objects - Symbolic Toolbox. Character. Logical. Numeric . Integer. Floating Point. multiple signed integer types. multiple unsigned integer types. single precision. double precision. complex. real.

yori
Download Presentation

CMPSC 201

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. CMPSC 201

  2. Other Kinds of Arrays Kinds of Data Stored in MATLAB Matrices Symbolic Objects - Symbolic Toolbox Character Logical Numeric Integer Floating Point multiple signed integer types multiple unsigned integer types single precision double precision complex real

  3. In this chapter we’ll learn about… • The different kinds of data used in MATLAB • Create and use both numeric and character arrays • Create multidimensional arrays, and access data in those arrays • Create and use cell and structure arrays

  4. Section 10.1Data Types • The primary data type (also called a class) in MATLAB is the array or matrix • Within the array, MATLAB supports a number of different secondary data types (classes) • The default is a double precision floating point number

  5. Kinds of Data Stored in MATLAB Matrices Symbolic Objects - Symbolic Toolbox Character Logical Numeric Integer Floating Point multiple signed integer types multiple unsigned integer types single precision double precision complex real MATLAB’s arrays can store different types of data

  6. Integer Floating Point multiple signed integer types multiple unsigned integer types single precision double precision There are a variety of array types to store the data MATLAB Array Types Character Arrays Logical Arrays Numeric Arrays Symbolic Arrays Cell Arrays Structure Arrays Most of these arrays can only hold information of one data type Cell and Structure arrays can store different types of data in the same array Other types, including user defined and JAVA types

  7. The difference between array types and data types may be confusing • Consider the following analogy

  8. There are lots of different places you could store your wealth They correspond to the array types

  9. There are lots of different kinds of wealth you might store in a bank They correspond to the data types

  10. Numeric Data Types • Numeric data is stored in numeric arrays • The default data type is double precision floating point • Every time you enter a number into MATLAB, the program assumes you’ve entered a double • MATLAB conforms to the IEEE standards that specify the meaning of the double data type

  11. When you define numeric values they default to doubles Each value in a double array needs 8 bytes of memory to store

  12. There are 6 values in the C array – therefore it requires 6 x 8 = 48 bytes of memory

  13. Value limitations • The biggest number you can store in a double can be found using the realmax function • The smallest number you can store in a double can be found using the realmin function

  14. Single Precision floating point numbers • This data type is a new feature in MATLAB 7 • Uses half the storage space of a double • Each value requires • 4 bytes = 32 bits

  15. The grid symbol indicates a numeric array – double, single or integer It’s necessary to use the single function to change the value of 5 (which is a double by default) into a single

  16. Value limitations • Since single precision numbers are allocated only half as much storage space, they can not cover as large a range of values as double numbers • The biggest number you can store in a single can be found using the realmax(‘single’) function • The smallest number you can store in a double can be found using the realmin(‘single’) function

  17. Engineers will rarely need to convert to single precision numbers, because today’s computers have plenty of storage space for most applications, and will execute most of the problems we pose in extremely short amounts of time

  18. When would you use the short data type instead of double values? • In some numerical analysis applications you may be able to improve the run time of a long problem by changing from double to single precision • However, round off error becomes more of a problem.

  19. Consider the harmonic series Shorthand for the harmonic series This series diverges -it just keeps getting bigger the more terms you add

  20. For large numbers of steps the results are different using double and single data types double single

  21. Why? • When the series gets big enough the value of 1/n is so small that the computer can’t distinguish it from 0 • This occurs at the value of realmin • Since doubles can differentiate between smaller numbers than singles the summation is valid for more steps

  22. Each of these types require a different amount of storage Integers Remember that 8 bits = 1 byte • Integer arrays are new to MATLAB 7 • Integers are stored in integer arrays

  23. You can determine the number of possible values allowed in the integer data type • Each of the integer types uses a different amount of storage, and can thus save different ranges of values • Determine the size range using • intmax and intmin

  24. When do we use integers • Integer arrays can be used to store image information • These arrays are often very large, but there are a limited number of colors used in many of these images to create the picture. • Storing them as unsigned integer arrays reduces the storage requirement dramatically.

  25. Complex numbers • Default is double • Twice as much storage is needed because the real and imaginary components must be stored • Could also be stored as a single or integer

  26. Character and String Data • Character arrays store character information • A character array is produced from a string

  27. Each character is a separate element in the array

  28. The fifth element of the H array is the letter y

  29. Any string represents a character array in MATLAB • Each character requires 2 bytes of storage

  30. The ‘abc’ symbol indicates a character array Spaces are characters too

  31. How are characters stored in MATLAB? • All information in computers is stored using a series of zeros and ones • ASCII – Used in small computers • EBCDIC – Used in mainframes and super computers • You can think of this list of 0’s and 1’s as a base two number

  32. Comparison between base 2 and base 10

  33. Every character stored using ASCII or EBCDIC code has both a binary representation and a decimal equivalent • When we ask MATLAB to change a character to a double, the number we get is the decimal equivalent in the ASCII coding system

  34. MATLAB includes functions to change data types Use the double function to convert to a double precision floating point number Use char to convert a number to a character

  35. You shouldn’t mix data types in calculations or in arrays If you attempt to create an array with both character and numeric data, the array defaults to all characters The square symbol is a character with a numeric equivalent of 3

  36. If you try to perform arithmetic with a combination of character and numeric data, MATLAB converts the character to its decimal equivalent Remember that a has a decimal equivalent of 97

  37. Symbolic Data • Covered in more detail in a separate chapter • The symbolic toolbox uses symbolic data to perform symbolic algebraic calculations • Create a symbolic variable using the sym function

  38. Storage requirements for symbolic vary, depending on how big the expression is.

  39. The cube symbol indicates a symbolic array Symbolic variables can be grouped into arrays, just like other data types

  40. Logical Data Types • Logical data can have only one of two values • True • False • MATLAB uses • 0 to represent false and • 1 to represent true

  41. The check mark indicates a logical array Although a logical array contains the information true and false, MATLAB represents it as 0 and 1 We don’t usually create logical arrays by entering true and false values. Usually they are the result of logical operations

  42. Notice that x and y are numeric arrays and z is a logical array We can interpret this result to mean that x>y is false for elements 1 and 3, and true for elements 2,3 and 5 These arrays are used in logical functions, and are not usually even seen by the user.

  43. Sparse Arrays • Both double precision arrays and logical arrays can be stored in either full matrices, or as sparse matrices. • Sparse matrices are “sparsely populated”, meaning many or most of the values in the array are zero • Identity matrices are examples of sparse matrices

  44. Sparse matrices require less space than the corresponding numeric or logical matrices • If we store sparse arrays in the full matrix format, it takes 8 bytes of storage for every data value, whether they are zeros or not • The sparse matrix format only stores the non-zero values, and remembers where they are • this strategy saves a lot of space

  45. Compare the size of N and P N is a 1000x1000 identity matrix P is the same matrix, stored using the sparse strategy

  46. Section 10.2Multidimensional Arrays • Sometimes you may want to store data in multidimensional arrays • Rows • Columns • Pages • Additional dimensions are possible

  47. columns rows pages Multidimensional arrays are grouped into pages

  48. Page 1 Page 2 Page 3 Page 4 Imagine that you would like to store each of these 4 two-dimensional arrays into 1 three-dimensional array with four pages.

  49. You must define each page separately. Read the first definition statement as “all the rows, all the columns, in page 1”

More Related