Matlab digits precision ruins the calculation. How to avoid it?

Adiel

I work on an image, and i normalized it by its maximum value (3165 in this case).

After some work, I wanted to do some operations with the original values of the pixels, so I multiplied back with 3165.

The problem is that because of the digits precision, not all the pixels get back their integer value. For example consider a pixel with value of 30:

a=30/3165

a =

    0.0095

>> b=a*3165

b =

   30.0000

>> b-30

ans =

   3.5527e-15

The error itself is very small, but i need to use it as an index for other vector, and it's not work, obviously.

Technically, i can use round after multiplying. It's a good solution, but ugly, and not feels like the appropriate way and good practice to solve this issue.

Someone has any other (more professional) ideas? Or this is the way to do it?

Thanks.

Divakar

The techniques mentioned below use eps and fix. These might be less "uglier" than rounding for your case. Let's look at these step by step with some codes and comments.

%%// Original indices
org_indices = [15 30 45 60]

%%// Scaled indices
scaled_indices = org_indices/3165

%%// Retrieved indices that are yet to be converted to exact integers
retrieved_indices = scaled_indices*3165

%%// Verify precision issue
check_ind1 = retrieved_indices(1) - 15

check_ind1 outputs 1.7764e-15 and is not exactly zero.

We can use eps that according to the official MATLAB documentation says - If d = eps(X), then d is the positive distance from abs(X) to the next larger in magnitude floating point number of the same precision as X.

Thus, we can calculate such distances/offsets as follows -

eps_val = eps(retrieved_indices)

eps_val could be thought of as window vector that determines within which if an element lies, must correspond to the integer that also lies within it. To make it easy for us, we can use the maximum of these window values for our decision making and to make it very safe, we can increase this width 10 times and let's call it max_window_width -

max_window_width = 10*max(eps_val)

This is not as ugly as rounding because rounding uses a window length of 1 because of its range [-0.5 0.5], whereas this one uses a window length of 7.1054e-14.

Finally, we can get the original indices back by adding this maximum width and then truncating the decimal portion using fix -

retrieved_indices2 = fix(retrieved_indices + max_window_width)

One can check back the values for exactness -

check_ind2 = retrieved_indices2(1) - 15

check_ind2 outputs 0.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

how to avoid a loop in a calculation of mean in matlab

From Dev

How to avoid calculation mistakes with primitives

From Dev

Maxima - internal numeric representation ruins calculation

From Dev

How to use fmod and avoid precision issues

From Dev

How to do the following calculation in Matlab?

From Dev

Matlab precision

From Dev

java algorithm calculation of precision

From Dev

Show digits after calculation?

From Dev

Matlab: How to matrix calculation with cell matrix?

From Dev

How to properly avoid copy pasting methods for single/double precision in CUDA

From Dev

How to Avoid Precision Errors While Creating Numpy Arrays?

From Dev

Rounding and trimming digits with specified precision

From Dev

input a float with two digits of precision

From Dev

input a float with two digits of precision

From Dev

Delphi wrong double precision calculation

From Dev

How can I avoid rounding errors in this triangle area calculation?

From Dev

How to avoid image display artifacts in Matlab?

From Dev

Matlab: How to avoid artefacts in filled contour plots

From Dev

How to convert decimal to IEEE double precision floating point binary in Matlab?

From Dev

How to get precision (number of digits past decimal) from a Ruby BigDecimal object?

From Dev

How do I format a number to have a fixed precision AND a fixed number of integral digits?

From Dev

Using matlab,how to find the last two digits of a decimal number?

From Dev

Matlab fwrite precision conversion

From Dev

matlab bsxfun numerical precision

From Dev

Precision of Fortran vs that of Matlab

From Dev

matlab double precision confusion

From Dev

Matlab fwrite precision conversion

From Dev

Precision of Fortran vs that of Matlab

From Dev

matlab double precision confusion

Related Related

  1. 1

    how to avoid a loop in a calculation of mean in matlab

  2. 2

    How to avoid calculation mistakes with primitives

  3. 3

    Maxima - internal numeric representation ruins calculation

  4. 4

    How to use fmod and avoid precision issues

  5. 5

    How to do the following calculation in Matlab?

  6. 6

    Matlab precision

  7. 7

    java algorithm calculation of precision

  8. 8

    Show digits after calculation?

  9. 9

    Matlab: How to matrix calculation with cell matrix?

  10. 10

    How to properly avoid copy pasting methods for single/double precision in CUDA

  11. 11

    How to Avoid Precision Errors While Creating Numpy Arrays?

  12. 12

    Rounding and trimming digits with specified precision

  13. 13

    input a float with two digits of precision

  14. 14

    input a float with two digits of precision

  15. 15

    Delphi wrong double precision calculation

  16. 16

    How can I avoid rounding errors in this triangle area calculation?

  17. 17

    How to avoid image display artifacts in Matlab?

  18. 18

    Matlab: How to avoid artefacts in filled contour plots

  19. 19

    How to convert decimal to IEEE double precision floating point binary in Matlab?

  20. 20

    How to get precision (number of digits past decimal) from a Ruby BigDecimal object?

  21. 21

    How do I format a number to have a fixed precision AND a fixed number of integral digits?

  22. 22

    Using matlab,how to find the last two digits of a decimal number?

  23. 23

    Matlab fwrite precision conversion

  24. 24

    matlab bsxfun numerical precision

  25. 25

    Precision of Fortran vs that of Matlab

  26. 26

    matlab double precision confusion

  27. 27

    Matlab fwrite precision conversion

  28. 28

    Precision of Fortran vs that of Matlab

  29. 29

    matlab double precision confusion

HotTag

Archive