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:
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) A_round -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