0

I was checking the similarity of two random matrices by using two methods ,after addition, sum of one elements of first matrix comes as .7095 and same .7095 for second matrix matrix but when tried to find the difference in sum ,instead of zero it was giving a value very close to zero .Later I checked in work space and find out that first number is actually 0.709485632903040 and the second is 0.709485632903037. It is extremely important for me to have the difference vector to be zero so as I am using that zero in my later stages of my program .If matlab do calculation for a precision in less than 4 or 5 digits I can achieve that. I want to limit the calculation only for 4 or 5 digits,I had used digits(4) but it is not working . I want matlab to do calculation in a precision up to 4 decimal places only(not for display,calculation inside matlab) ,Is there is a way to do that??

agentp
  • 6,849
  • 2
  • 19
  • 37
Abhishek
  • 81
  • 8
  • edited your tags. almost all of them made no sense – agentp Sep 07 '15 at 14:32
  • 5
    nope ... you are still going to have the same problem (possibly amplified) if you reduce the calculation precision. The way to go is to do all your _calculations_ at maximum precision, then only when it comes to _comparisons_ (`A-B==0` for example), you introduce the notion of tolerance, which can be done in 2 ways: (1. _preferred_) Define a tolerance and compare to that: `tol=1e-5 ; abs(A-B)<=tol ;`, or (2) Round at this stage: `round(A,5)-round(B,5)==0`. – Hoki Sep 07 '15 at 14:42
  • I think the problem could be related to the question [why is 24.0000 not equal to 24 0000 in matlab](http://stackoverflow.com/questions/686439/why-is-24-0000-not-equal-to-24-0000-in-matlab?lq=1), regarding floating point precision. – patrik Sep 08 '15 at 07:12

0 Answers0