How to round a matrix to n decimal places

Hi,
I am trying to round the matrix to 2 decimal places a=[1.232323 2.23232323; 3.23232323 4.2323232332] round(a,2)
I am getting the error too many input arguments. Is there a work around?

 Réponse acceptée

Star Strider
Star Strider le 4 Juil 2015
You can create your own version of the ‘new’ round:
roundn = @(x,n) round(x*10^n)./10^n;
q = roundn(pi,2)
q =
3.14

5 commentaires

Yasmin Samy
Yasmin Samy le 9 Mai 2019
Hi!
This worked fine but for some reason i get trailing zeros in the vector. the original data is input as 4 decimal places. I would like to display the data in the table with 2 decimal places, but i keep getting trailing zeros. Any idea how i can solve this??
Thanks
My pleasure.
Use the format (link) function.
Specifically:
format bank
or
format short g
would likely do what you want.
Note that table objects usually inherit the existing format settings.
Yasmin Samy
Yasmin Samy le 9 Mai 2019
I tried that but in the uitable it totally ignores that and i still get those trailing zeros. Besides bank is 2d and short g is like 5 decimal places.
Yasmin Samy
Yasmin Samy le 9 Mai 2019
Is there no way to round and truncate maybe the numbers?
Yasmin Samy
Yasmin Samy le 9 Mai 2019
Found it!!!
after rounding, convert num2cell and then use
fun3 = @(x) sprintf('%0.3f', x);

Connectez-vous pour commenter.

Plus de réponses (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by