I am writing a program where I need to delete duplicate points stored in a matrix. The problem is that when it comes to check whether those points are in the matrix, MATLAB can't recognize them in the matrix although they exist.

In the following code, `intersections`

function gets the intersection points:

```
[points(:,1), points(:,2)] = intersections(...
obj.modifiedVGVertices(1,:), obj.modifiedVGVertices(2,:), ...
[vertex1(1) vertex2(1)], [vertex1(2) vertex2(2)]);
```

The result:

```
>> points
points =
12.0000 15.0000
33.0000 24.0000
33.0000 24.0000
>> vertex1
vertex1 =
12
15
>> vertex2
vertex2 =
33
24
```

Two points (`vertex1`

and `vertex2`

) should be eliminated from the result. It should be done by the below commands:

```
points = points((points(:,1) ~= vertex1(1)) | (points(:,2) ~= vertex1(2)), :);
points = points((points(:,1) ~= vertex2(1)) | (points(:,2) ~= vertex2(2)), :);
```

After doing that, we have this unexpected outcome:

```
>> points
points =
33.0000 24.0000
```

The outcome should be an empty matrix. As you can see, the first (or second?) pair of `[33.0000 24.0000]`

has been eliminated, but not the second one.

Then I checked these two expressions:

```
>> points(1) ~= vertex2(1)
ans =
0
>> points(2) ~= vertex2(2)
ans =
1 % <-- It means 24.0000 is not equal to 24.0000?
```

What is the problem?

More surprisingly, I made a new script that has only these commands:

```
points = [12.0000 15.0000
33.0000 24.0000
33.0000 24.0000];
vertex1 = [12 ; 15];
vertex2 = [33 ; 24];
points = points((points(:,1) ~= vertex1(1)) | (points(:,2) ~= vertex1(2)), :);
points = points((points(:,1) ~= vertex2(1)) | (points(:,2) ~= vertex2(2)), :);
```

The result as expected:

```
>> points
points =
Empty matrix: 0-by-2
```

## Best Solution

The problem you're having relates to how floating-point numbers are represented on a computer. A more detailed discussion of floating-point representations appears towards the end of my answer (The "Floating-point representation" section). The

TL;DRversion: because computers have finite amounts of memory, numbers can only be represented with finite precision. Thus, the accuracy of floating-point numbers is limited to a certain number of decimal places (about 16 significant digits for double-precision values, the default used in MATLAB).## Actual vs. displayed precision

Now to address the specific example in the question...

whileIf you want to see the full precision, you should either issue the`24.0000`

and`24.0000`

aredisplayedin the same manner, it turns out that they actually differ by very small decimal amounts in this case. You don't see it because MATLAB only displays 4 significant digits by default, keeping the overall display neat and tidy.`format long`

command or view a hexadecimal representation of the number:## Initialized values vs. computed values

Since there are only a finite number of values that can be represented for a floating-point number, it's possible for a computation to result in a value that falls between two of these representations. In such a case, the result has to be rounded off to one of them. This introduces a small machine-precision error. This also means that initializing a value directly or by some computation can give slightly different results. For example, the value

`0.1`

doesn't have anexactfloating-point representation (i.e. it gets slightly rounded off), and so you end up with counter-intuitive results like this due to the way round-off errors accumulate:## How to correctly handle floating-point comparisons

Since floating-point values can differ by very small amounts, any comparisons should be done by checking that the values are within some range (i.e. tolerance) of one another, as opposed to exactly equal to each other. For example:

will display "Equal!".

You could then change your code to something like:

## Floating-point representation

A good overview of floating-point numbers (and specifically the IEEE 754 standard for floating-point arithmetic) is

What Every Computer Scientist Should Know About Floating-Point Arithmeticby David Goldberg.A binary floating-point number is actually represented by three integers: a sign bit

`s`

, a significand (or coefficient/fraction)`b`

, and an exponent`e`

. For double-precision floating-point format, each number is represented by 64 bits laid out in memory as follows:The real value can then be found with the following formula:

This format allows for number representations in the range 10^-308 to 10^308. For MATLAB you can get these limits from

`realmin`

and`realmax`

:Since there are a finite number of bits used to represent a floating-point number, there are only so many finite numbers that can be represented within the above given range. Computations will often result in a value that doesn't exactly match one of these finite representations, so the values must be rounded off. These machine-precision errors make themselves evident in different ways, as discussed in the above examples.

In order to better understand these round-off errors it's useful to look at the relative floating-point accuracy provided by the function

`eps`

, which quantifies the distance from a given number to the next largest floating-point representation:Notice that the precision is

relativeto the size of a given number being represented; larger numbers will have larger distances between floating-point representations, and will thus have fewer digits of precision following the decimal point. This can be an important consideration with some calculations. Consider the following example:Note that when we shift the values of

`x`

from the range`[0 1]`

to the range`[10000 10001]`

, compute a mean, then subtract the mean offset for comparison, we get a value that differs for the last 3 significant digits. This illustrates how an offset or scaling of data can change the accuracy of calculations performed on it, which is something that has to be accounted for with certain problems.