Documentation Center

  • Trial Software
  • Product Updates


Rational output




S = rats(X) returns a string containing the rational approximations to the elements of X using the default string length of 13. The rational output is the same as that produced by format rat.

rats returns asterisks for elements that cannot be printed in the allotted space, but which are not negligible compared to the other elements in X.


S = rats(X,strlen) returns a string of length strlen. The rational approximation uses a tolerance that is inversely proportional to string length.


expand all

Rational Representation of Matrix

Create a 4-by-4 matrix.

format short
X = hilb(4)
X =

    1.0000    0.5000    0.3333    0.2500
    0.5000    0.3333    0.2500    0.2000
    0.3333    0.2500    0.2000    0.1667
    0.2500    0.2000    0.1667    0.1429

View the rational representation of the matrix using rats.

R = rats(X)
R =

       1            1/2           1/3           1/4     
      1/2           1/3           1/4           1/5     
      1/3           1/4           1/5           1/6     
      1/4           1/5           1/6           1/7     

The result is the same as using format rat.

Adjust Output String Length

Find the rational representation of pi with the default string length and approximation tolerance.

ans =


The result is the same as using format rat.

Adjust the string length of the output, which also adjusts the approximation tolerance.

ans =


The resulting rational approximation has greater accuracy. As the string length increases, the tolerance decreases.

Adjust the string length again to achieve greater accuracy.

ans =


The resulting approximation agrees with pi to 10 decimal places.

Input Arguments

expand all

X — Input arraynumeric array


Data Types: single | double

strlen — String lengthpositive integer

String length, specified as a positive integer. The default string length is 13, which allows for 6 elements in 78 spaces.

Output Arguments

expand all

S — Rational outputstring

Rational output, returned as a string.

More About

expand all


The format rat command uses the same internal algorithm, with the default strlen, as rats.

rats obtains rational approximations with [N,D] = rat(X,tol), where tol is min(10^(-(strlen-1)/2)*norm(X(isfinite(X)),1),.1). Thus, the tolerance is inversely proportional to the string length, strlen.

See Also


Was this topic helpful?