Technical Solutions
How does the COLON operator work?
Date Last Modified: Friday, June 26, 2009
| Solution ID: |
|
1-4FLI96 |
| Product: |
|
MATLAB |
| Reported in Release: |
|
No Release |
| Platform: |
|
Windows |
| Operating System: |
|
Windows XP SP2 |
Subject:
How does the COLON operator work?
Problem Description:
I want to know how the COLON operator works:
For example,
v = a:d:b
Is vector 'v' calculated by repeated addition of multiples of 'd' to a?
Solution:
v = a:d:b is not constructed using repeated addition as that might accumulate round off errors. This is especially true for numbers that cannot be represented exactly in binary floating point, for instance the number 0.1. For instance, executing the following in MATLAB:
0.1+0.1+0.1 == 0.3
will not return true due to such round-off errors. To counteract such error accumulation, the algorithm of the COLON operator dictates that:
i) The first half of the output vector (in this example ‘v’) is calculated by adding integer multiples of the step ‘d’ to the left-hand endpoint ‘a’. ii) The second half is calculated by subtracting multiples of the step ‘d’ from the right-hand endpoint.
The COLON operator ensures symmetry of the output vector about its midpoint and also that the round off error is minimized at both endpoints. For example, even though 0.01 is not exactly represented in binary,
v = -1 : 0.01 : 1
consists of 201 floating points numbers centered symmetrically about zero.
For more detailed information on how the COLON operator works, refer to the attached M-file (COLONOP). This M-file mimics closely the behavior of the COLON operator.
|
Related Documents/Files: