Jan Paudel
Jan Paudel

Reputation: 11

How to fix the decimal place of matrix elements in matlab?

I have a matrix of order 3 x 3, and all elements of matrix are up to 6 decimal place. I want to display this elements of matrix only up to 5 decimal place. I used format short and format long, but it gives either 4 or 15 decimal places. Is there a command that gives up to any particular decimal places? I have idea for a single number but could not solve for all entries of a matrix.

Upvotes: 1

Views: 2254

Answers (1)

Suever
Suever

Reputation: 65430

The builtin format options cannot handle this. You'll instead want to use fprintf or num2str (with a format specifier) to force the appearance of the number

data = rand(3) * 100;
num2str(data,'%12.5f')

%   20.42155     3.95486    91.50871
%    9.28906    87.24924    72.61826
%   47.43655    95.70325    94.41092

If you want to make this the default display at the command line you could overload the builtin display method for double but I would not recommend that.

Alternately, you can use vpa to specify the number of significant digits to display (note that the second input is the number of significant digits and not the number of numbers after the radix point).

vpa(data, 5)

Upvotes: 3

Related Questions