Matlab/Octave 輸出結果用分數表示

1 使用format命令

format rat
eg:
A=[0 1/3 1/3 1/3; 1/3 0 1/3 1/3; 1/3 1/3 0 1/3; 1/3 1/3 1/3 0]
A =

          0        1/3        1/3        1/3
        1/3          0        1/3        1/3
        1/3        1/3          0        1/3
        1/3        1/3        1/3          0

2 Matlab/Octave默認精度SHORT

SHORT就是保留5位小數(Matlab(4爲小數)、Octave(5爲小數))。如果想要轉換成原來的樣子,直接輸入format即可,不要加參數。注意:這只是影響結果顯示,不會影響內部計算和存儲。

3 其他格式,參見 help format

LONG: 小數位數15位。下面是所有的格式:

 'short'
          Fixed point format with 5 significant figures (default).

     'long'
          Fixed point format with 16 significant figures.

          As with the 'short' format, Octave will switch to an
          exponential 'e' format if it is unable to format a matrix
          properly using the current format.

     'short e'
     'long e'
          Exponential format.  The number to be represented is split
          between a mantissa and an exponent (power of 10).  The
          mantissa has 5 significant digits in the short format.  In the
          long format, double values are displayed with 16 significant
          digits and single values are displayed with 8.  For example,
          with the 'short e' format, 'pi' is displayed as '3.1416e+00'.

     'short E'
     'long E'
          Identical to 'short e' or 'long e' but displays an uppercase
          'E' to indicate the exponent.  For example, with the 'long E'
          format, 'pi' is displayed as '3.141592653589793E+00'.

     'short g'
     'long g'
          Optimally choose between fixed point and exponential format
          based on the magnitude of the number.  For example, with the
          'short g' format, 'pi .^ [2; 4; 8; 16; 32]' is displayed as

               ans =

                     9.8696
                     97.409
                     9488.5
                 9.0032e+07
                 8.1058e+15

     'short eng'
     'long eng'
          Identical to 'short e' or 'long e' but displays the value
          using an engineering format, where the exponent is divisible
          by 3.  For example, with the 'short eng' format, '10 * pi' is
          displayed as '31.416e+00'.

     'long G'
     'short G'
          Identical to 'short g' or 'long g' but displays an uppercase
          'E' to indicate the exponent.

     'free'
     'none'
          Print output in free format, without trying to line up columns
          of matrices on the decimal point.  This is a raw format
          equivalent to the C++ code 'std::cout << VARIABLE'.  In
          general, the result is a presentation with 6 significant
          digits where unnecessary precision (such as trailing zeros for
          integers) is suppressed.  Complex numbers are formatted as
          numeric pairs like this '(0.60419, 0.60709)' instead of like
          this '0.60419 + 0.60709i'.

     The following formats affect all numeric output (floating point and
     integer types).

     "+"
     "+" "CHARS"
     'plus'
     'plus CHARS'
          Print a '+' symbol for matrix elements greater than zero, a
          '-' symbol for elements less than zero, and a space for zero
          matrix elements.  This format can be useful for examining the
          sparsity structure of a large matrix.  For very large matrices
          the function 'spy' which plots the sparsity pattern will be
          clearer.

          The optional argument CHARS specifies a list of 3 characters
          to use for printing values greater than zero, less than zero,
          and equal to zero.  For example, with the format "+" "+-.",
          the matrix '[1, 0, -1; -1, 0, 1]' is displayed as

               ans =

               +.-
               -.+

     bank
          Print variable in a format appropriate for a currency (fixed
          format with two digits to the right of the decimal point).
          Only the real part of a variable is displayed, as the
          imaginary part makes no sense for a currency.

     native-hex
          Print the hexadecimal representation of numbers as they are
          stored in memory.  For example, on a workstation which stores
          8 byte real values in IEEE format with the least significant
          byte first, the value of 'pi' when printed in 'native-hex'
          format is '400921fb54442d18'.

     hex
          The same as 'native-hex', but always print the most
          significant byte first.

     native-bit
          Print the bit representation of numbers as stored in memory.
          For example, the value of 'pi' is

               01000000000010010010000111111011
               01010100010001000010110100011000

          (shown here in two 32 bit sections for typesetting purposes)
          when printed in native-bit format on a workstation which
          stores 8 byte real values in IEEE format with the least
          significant byte first.

     bit
          The same as 'native-bit', but always print the most
          significant bits first.

     rat
          Print a rational approximation, i.e., values are approximated
          as the ratio of small integers.  For example, with the 'rat'
          format, 'pi' is displayed as '355/113'.

     The following two options affect the display of all matrices.

     'compact'
          Remove blank lines around column number labels and between
          matrices producing more compact output with more data per
          page.

     'loose'
          Insert blank lines above and below column number labels and
          between matrices to produce a more readable output with less
          data per page.  (default).
發佈了65 篇原創文章 · 獲贊 62 · 訪問量 8萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章