Question About Number Format in MATLAB Command Window

2 Ansichten (letzte 30 Tage)
ercan duzgun
ercan duzgun am 14 Jan. 2021
Kommentiert: ercan duzgun am 14 Jan. 2021
% I would like to display a number as a short format. I already executed this command:
format short
% However it doesn't work for some cases. For example, I receive this result:
>> pi
ans =
3.1416
% But I have another variable which I got from solutions of a polynomial.("roots" command)
% If I display those solutions,
% I get long number results. It looks like this:
>> format short
>> disp(x1_sols)
0.74613506773662727719908766254638
0.74624784618189352426025884090749
0.77735277675703701413872152754508
0.82647611867148647532781596815636
1.0679165995745538795723120147744
1.3550488761820195655871374504513
% How can I get short form of this?
% Thank you in advance.

Akzeptierte Antwort

John D'Errico
John D'Errico am 14 Jan. 2021
Bearbeitet: John D'Errico am 14 Jan. 2021
These are numbers from the symbolic toolbox. That toolbox does not use the format tools to display numbers (though perhaps arguably it could, but that would introduce a whole new set of problms.) You can use vpa to do that.
XX = vpa(1./sym([2;3;7;11;13]))
XX =
0.5
0.33333333333333333333333333333333
0.14285714285714285714285714285714
0.090909090909090909090909090909091
0.076923076923076923076923076923077
vpa(XX,4)
ans =
0.5
0.3333
0.1429
0.09091
0.07692
  3 Kommentare
Walter Roberson
Walter Roberson am 14 Jan. 2021
Or you can use double(), which would probably be more appropriate.
ercan duzgun
ercan duzgun am 14 Jan. 2021
Thank you @Walter Roberson . Your suggestion is a better solution for me.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Graphics Object Programming finden Sie in Help Center und File Exchange

Tags

Produkte


Version

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by