Data Types


Data Types

matlab



Data stored in memory

Computer memory consists of bits. It is arranged in a line, in groups of eight, known as bytes.

The bits in each byte contain binary values. Set to 1 or 0.

A pattern of bits can be interpreted in different ways. It depends on what data type we assume.

Simple data types

The bit pattern 0100 0001 can be viewed as:

  • An integer, $ 2^{6} + 2^{0} = 64 + 1 = 65 $
  • A character, need a look up table. e.g ASCII table. i.e. 0100 0001 = A
  • There are also other data types…
Pattern Decimal ASCII Character
0011 1100 62 >
0011 1111 63 ?
0100 0000 64 @
0100 0001 65 A
0100 0010 65 B

Integers

If we use a single byte and want to represent unsigned integers (e.g. 0,1,2,3,…), then we can use binary patterns:

0000 0000 0
0000 0001 1
0000 0010 2
0000 0011 3
1111 1101 253
1111 1110 254
1111 1111 255

If we use a single byte and want to represent signed integers (e.g, …,–3,–2,–1,0,1,2,3,…). We need one bit for the sign (+ -). So, fewer bits for the number’s magnitude. i.e. can only represent numbers with a smaller magnitude.

Integers in Matlab

Matlab has two single byte integer data types:

  • uint8 - “unsigned integer, 8 bits”
  • int8 - “signed integer, 8 bits”

The range of these types can be checked using built in functions intmin & intmax.

For larger integers, it is possible to use more bytes

>> intmin('uint8')
ans =
    0
>> intmax('uint8')
ans =
    255
>> intmin('int8')
ans =
    -128
>> intmax('int8')
ans =
    127
Larger integers

For larger integers, we can use more bytes:

  • int16 uint16: 2 bytes
  • int32 uint32: 4 bytes
  • int64 uint64: 8 bytes

This means that it is possible to represent a wider range of integers, e.g.

>> intmax('uint16')
ans =
    65535
>> intmin('int64')
ans =
    -9223372036854775808

Floating point numbers Real Numbers

These can be represented using scientific notation. This is useful for non-integers and very large/small numbers. e.g.

15! = 130,767,436,800 ≈ 1.3077 × 1012

1 atomic mass ≈ 1.6605 × 10-27kg

We have a mantissa, -7.2101, and an exponent, 7

7.2101 × 107
Floating point numbers in Matlab

These can use:

  • single: 4 bytes (32 bits)
  • double: 8 bytes (64 bits)

With the allotted bits, we need encode:

  • The Mantissa and its sign
  • The Exponent and its sign

Both the Mantissa and Exponent can use:

  • single: 4 bytes (32 bits)
  • double: 8 bytes (64 bits)

Both these data types are signed by default. It is possible to check the smallest and largest possible values with built in realmin and realmax functions:

>> realmax('double')
ans =
    1.7977e+308
>> realmin('double')
ans =
    2.2251e-308
Special floating point numbers

In Matlab, specific values of double can be used to represent:

  • Infinity
  • Not-a-Number (Nan)

These values can be found with built in functions:

>> isinf(a)
ans =     1
>> isnan(b)
ans =     1

>> a = 2 / 0
a =
  Inf

>> b = 0 / 0
b =
  NaN

>> c = str2double('blah')
c =
  NaN

Logical data type Boolean

Can have two values: true or false. This is implemented as 1 or 0. A logical value can be created by evaluating a conditional test.

Summary of data types

Character   char
Boolean   logical
Numeric unsigned integers uint8 uint16 uint32 uint64
Numeric signed integers int8 int16 int32 int64
Numeric signed floating point single double

Arrays

We can make arrays from basic types:

>> n = 3:-1:0
n =
  3   2   1   0
>> r = linspace(0, pi, 3)
r =
  0   1.5708   3.1416

Matrix Operations

2D numeric arrays are known as matrices. Creating matrices allows you to perform a range of linear algebra operations:

c = a*b
d = a+b
e = inv(a)
f = transpose(b)

Character Arrays

Arrays from basic types can make character arrays. Character arrays with a single row are described as strings:

>> s1 = ['c' 'a' 't']
s1 =
   cat

>> s2 = 'dog'
s2 =
   dog

>> whos s1 s2
  Name      Size     Bytes     Class

  s1        1x3      6         char
  s2        1x3      6         char

Logical Arrays

Arrays from basic types can make logical arrays:

>> x = 1:3
x =  1  2  3

>> y = 3:-1:1
y =  3  2  1

>> whereEqual = ( x == y )
whereEqual =  0  1  0

>> whos whereEqual
                Name           Size       Bytes    Class
                whereEqual     1x3        3        logical

Converting between the types Casting

It is possible to convert from/to numeric from/to:

  • logical
  • character

However it is not possible to convert from/to logical from/to character

Converting between numeric and character types

Based on codes used to represent the characters in the look up table:

>> char(66)
ans =
    B

>> double('B')
ans =
    66
Converting between Numeric and Logical types

Non-zero numbers get converted to true. Only zero gets converted to false:

>> logical(1)
ans =
    1       true

>> logical(3.5)
ans =
    1       true

>> logical(-12)
ans =
    1       true

>> logical(0)
ans =
    0       false

Converting the other way, i.e. logical values to numeric:

>> double(true)
ans =
    1
>> double(false)
ans =
    0

Explicit conversion

This is where the specific functions, double, char & logical are used to convert between data types.

Implicit conversion

This is where Matlab converts data automatically without using a dedicated function. e.g. numeric to character:

>> x = 70;
>> str = ['This is number seventy: ' x];
>> disp(str)
   This is number seventy: F

This is not what the operator wanted as it converted the number x based on character codes to F. In order to get a string representation of the number ’70’, then the function num2str should be used.

The same the problem arises with the following example, character to numeric:

>> '3.142' + 1
ans =
    52 47 50 53 51

This implicitly converts string (char array) ’3.142’ to an array of 5 numbers based on character codes. It adds one to each. In order to operate on the number 3.142, we need to explicitly convert the string representation:

>> str2double('3.142') + 1
ans =
    4.1420


return  link
Written by Tobias Whetton