MATLAB: How to round up all components of a matrix to nearest ‘k’ decimal

matrixround up

Hi,
Does anyone know how I can change this:
d =
0.017869069824194415755502531669223
-0.0050797537564829351997036551331211
-0.0077677772808408665487476505869037
to this:
d =
0.018
-0.005
-0.008
Actually I want to round it up in the 3rd decimal. Thanks.

Best Answer

vpa(d,3)
We can tell from the number of decimal places that you show that you must be using symbolic numbers, so you can use vpa() to evaluate to a specific number of decimal places.
There is no way to round double precision numbers to a particular number of decimal places, because 0.1 is not exactly representable as a finite binary floating point number, for the same reasons that 1/7 is not exactly representable as a finite decimal number.