Chapter 08 - Chapter 8 Data Types Every modern programming...

Info icon This preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
Chapter 8 Data Types Every modern programming language provides means for storing numbers in variables, operating on them, and printing them. Matlab is no exception, and we have seen countless examples of numbers being stored, operated on, and printed. The examples suggest that these numbers are the same as the real numbers and complex numbers of mathematics, where there is no upper limit to absolute value of a number and no lower limit to the absolute difference between two numbers. On the other hand, since the space of mathematical numbers is infinite, while the memory of a computer is finite, it is clear that the use of a computer must impose some limits. It does. There is an upper limit to the size of a number on the computer, and there is a lower limit to the absolute difference between two numbers. The set of numbers that can be represented by a Matlab variable is, therefore, finite. We are about to learn that that there is more than one type of number in Matlab, and that each type provides a different set of allowed numbers. Furthermore, we will learn that there are types other than numeric types that hold other data. These other types cannot be used in arithmetic operations, but do allow other operations. A data type is in fact completely determined by the set of all possible values it can have and the set of operations that can be performed on it. Indeed, in the formalism of computer science, a data type , or simply, type , is defined as a set values and a set of operations that can be performed on those values. It is common to employ variables of several different types in a single function. It is even possible to use variables of different types in the same operations. Matlab does, however, enforce one rule of uniformity: All elements of a given array must be of the same type. We call the types of these elements elementary types . Thus, when we speak of the type of a variable that is not a scalar, it is not necessary to specify the type of each element in the array. We need specify only one elementary type. A complete description of the type of an array will include: the number of dimensions, the size in each dimension, and the type of the elements. In Matlab there are always two or more sizes because the number of dimensions is always 2 or more (even a scalar is a one by one matrix!) Example descriptions of types are x is a 1 by 1 matrix with elements of type double x is a scalar of type double y is a row vector with 10 elements of type single y is a 1 by 10 vector with elements of type single A is a 2 by 3 matrix with elements of type int8 B is a 2 by 3 by 4 by 5 array with elements of type logical
Image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
The class function To determine the data type of a Matlab variable, use the function class . For example, >> x = 14 x = 14 >> class(x) ans = double (Matlab uses the name class and reserves the name type for a command that prints out the contents of an M-file.) Values have types, even when they are not stored in a variable. Thus, >> class(14) ans = double >> class('Mary had a little lamb.') ans = char
Image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern