Integer and data representation in Matlab

Number formatting can be crucial when presenting data. While a simple topic, Matlab has provided many built in commands that can make your analysis easier. This tutorial will cover round, fix, ceil, floor and format.

Let’s begin with a simple 3 by 3 matrix A.

A =
  -5.7000 + 0.6500i   2.7800             2.3300          
        0 - 2.4000i        0             1.5000          
   2.5000            -0.1000                  0 + 2.4000i

By default all numbers are displayed with up to 5 digits of precision. If you want to change the output to display additional decimal places (15) you may use:

format long

Using “format short” or “format” can return to the default display length, with neither option affecting the display of integers. Regardless of the display length, all calculations are performed similarly. There are a few other display options for format including hex, +, compact and loose, which are explained further at Mathworks .

Using the predefined round function will perform rounding to the nearest integer as we might remember from our middle school days. All numbers greater than our equal to n.5 are rounded up to the next integer n+1 and all numbers less than n.5 are rounded down to n:

A_round = round(A)
  -6.0000 + 1.0000i   3.0000             2.0000          
        0 - 2.0000i        0             2.0000          
   3.0000                  0                  0 + 2.0000i

The command “round”, and the following three rounding options work independently on complex numbers, with the rule for “round” applied to both the real and imaginary parts.

If we wanted all values of A to be integers rounded towards positive infinity, we can use Matlab’s ceil function:

A_ceil = ceil(A)
A_ceil = 
  -5.0000 + 1.0000i   3.0000             3.0000          
        0 - 2.0000i        0             2.0000          
   3.0000                  0                  0 + 3.0000i

As you can see all the integer values are rounded up to the next largest integer. The opposite of “ceil” is “floor”, with all numbers rounded down to the next smallest integer.

A_floor = floor(A)
A_floor =
  -6.0000             2.0000             2.0000          
        0 - 3.0000i        0             1.0000          
   2.0000            -1.0000                  0 + 2.0000i

Finally, the command “fix” will round all numbers, real and imaginary, towards zero (their absolute magnitude is reduced towards the next smallest integer).

A_fix = fix(A)
A_fix =

  -5.0000             2.0000             2.0000          
        0 - 2.0000i        0             1.0000          
   2.0000                  0                  0 + 2.0000i

2 thoughts on “Integer and data representation in Matlab

  1. I’ve been studying your entries all the way through my morning break, along with I will have to admit the entire paragraph has been extremely enlightening as well as really well written. I assumed I’d mean you be capable to recognize that for a few description why this weblog does now not view smartly in Web Explorer 8. I wish Microsoft would forestall converting their software. I’ve a question for you. could you thoughts exchanging blog roll links? That will be in reality neat!

Leave a Reply

Your email address will not be published. Required fields are marked *