home *** CD-ROM | disk | FTP | other *** search
/ Geek Gadgets 1 / ADE-1.bin / ade-bin / info / octave.info-6 < prev    next >
Encoding:
GNU Info File  |  1996-10-12  |  48.6 KB  |  1,332 lines

  1. This is Info file octave.info, produced by Makeinfo-1.64 from the input
  2. file octave.texi.
  3.  
  4.    Copyright (C) 1993, 1994, 1995 John W. Eaton.
  5.  
  6.    Permission is granted to make and distribute verbatim copies of this
  7. manual provided the copyright notice and this permission notice are
  8. preserved on all copies.
  9.  
  10.    Permission is granted to copy and distribute modified versions of
  11. this manual under the conditions for verbatim copying, provided that
  12. the entire resulting derived work is distributed under the terms of a
  13. permission notice identical to this one.
  14.  
  15.    Permission is granted to copy and distribute translations of this
  16. manual into another language, under the above conditions for modified
  17. versions.
  18.  
  19. 
  20. File: octave.info,  Node: Basic Input and Output,  Next: C-Style I/O Functions,  Prev: Input and Output,  Up: Input and Output
  21.  
  22. Basic Input and Output
  23. ======================
  24.  
  25.    Since Octave normally prints the value of an expression as soon as it
  26. has been evaluated, the simplest of all I/O functions is a simple
  27. expression.  For example, the following expression will display the
  28. value of pi
  29.  
  30.      octave:13> pi
  31.      pi = 3.1416
  32.  
  33.    This works well as long as it is acceptable to have the name of the
  34. variable (or `ans') printed along with the value.  To print the value
  35. of a variable without printing its name, use the function `disp'.  For
  36. example, the following expression
  37.  
  38.      disp ("The value of pi is:"), disp (pi)
  39.  
  40. will print
  41.  
  42.      The value of pi is:
  43.      3.1416
  44.  
  45. Note that the output from `disp' always ends with a newline.
  46.  
  47.    A simple way to control the output format is with the `format'
  48. statement.  For example, to print more digits for pi you can use the
  49. command
  50.  
  51.      format long
  52.  
  53. Then the expression above will print
  54.  
  55.      The value of pi is:
  56.      3.14159265358979
  57.  
  58.    Here is a summary of the options for `format':
  59.  
  60. `short'
  61.      This is the default format.  Octave will try to print numbers with
  62.      at least 5 significant figures within a field that is a maximum of
  63.      10 characters wide.
  64.  
  65.      If Octave is unable to format a matrix so that columns line up on
  66.      the decimal point and all the numbers fit within the maximum field
  67.      width, it switches to an `e' format.
  68.  
  69. `long'
  70.      Octave will try to print numbers with at least 15 significant
  71.      figures within a field that is a maximum of 24 characters wide.
  72.  
  73.      As will the `short' format, Octave will switch to an `e' format if
  74.      it is unable to format a matrix so that columns line up on the
  75.      decimal point and all the numbers fit within the maximum field
  76.      width.
  77.  
  78. `long e'
  79. `short e'
  80.      The same as `format long' or `format short' but always display
  81.      output with an `e' format.  For example, with the `short e'
  82.      format, pi is displayed as
  83.  
  84.            3.14e+00
  85.  
  86. `long E'
  87. `short E'
  88.      The same as `format long e' or `format short e' but always display
  89.      output with an uppercase `E' format.  For example, with the `long
  90.      E' format, pi is displayed as
  91.  
  92.            3.14159265358979E+00
  93.  
  94. `free'
  95. `none'
  96.      Print output in free format, without trying to line up columns of
  97.      matrices on the decimal point.  This also causes complex numbers
  98.      to be formatted like this `(0.604194, 0.607088)' instead of like
  99.      this `0.60419 + 0.60709i'.
  100.  
  101. `bank'
  102.      Print in a fixed format with two places to the right of the decimal
  103.      point.
  104.  
  105. `+'
  106.      Print a `+' symbol for nonzero matrix elements and a space for zero
  107.      matrix elements.  This format can be very useful for examining the
  108.      structure of a large matrix.
  109.  
  110.    The `input' function may be used for prompting the user for a value
  111. and storing the result in a variable.  For example,
  112.  
  113.      input ("Pick a number, any number! ")
  114.  
  115. prints the prompt
  116.  
  117.      Pick a number, any number!
  118.  
  119. and waits for the user to enter a value.  The string entered by the user
  120. is evaluated as an expression, so it may be a literal constant, a
  121. variable name, or any other valid expression.
  122.  
  123.    Currently, `input' only returns one value, regardless of the number
  124. of values produced by the evaluation of the expression.
  125.  
  126.    If you are only interested in getting a literal string value, you can
  127. call `input' with the character string `s' as the second argument.
  128. This tells Octave to return the string entered by the user directly,
  129. without evaluating it first.
  130.  
  131.    Because there may be output waiting to be displayed by the pager, it
  132. is a good idea to always call `fflush (stdout)' before calling `input'.
  133. This will ensure that all pending output is written to the screen
  134. before your prompt.  *Note C-Style I/O Functions::.
  135.  
  136.    The second input function, `keyboard', is normally used for simple
  137. debugging.  Using `keyboard', it is possible to examine the values of
  138. variables within a function, and to assign newassign new variables Like
  139. `input', it prompts the user for input, but no value is returned, and
  140. it continues to prompt for input until the user types `quit', or `exit'.
  141.  
  142.    If `keyboard' is invoked without any arguments, a default prompt of
  143. `debug> ' is used.
  144.  
  145.    For both of these functions, the normal command line history and
  146. editing functions are available at the prompt.
  147.  
  148.    To save variables in a file, use the `save' command.  For example,
  149. the command
  150.  
  151.      save data a b c
  152.  
  153. saves the variables `a', `b', and `c' in the file `data'.
  154.  
  155.    The SAVE command can read files in Octave's text and binary formats
  156. as well as MATLAB's binary format.  You can specify the default format
  157. with the built-in variable DEFAULT_SAVE_FORMAT using one of the
  158. following values: `"binary"' or `"mat-binary"'.  The initial default
  159. save format is Octave's text format.
  160.  
  161.    You can use the built-in variable `save_precision' to specify the
  162. number of digits to keep when saving data in text format.
  163.  
  164.    The list of variables to save may include wildcard patterns
  165. containing the following special characters:
  166. `?'
  167.      Match any single character.
  168.  
  169. `*'
  170.      Match zero or more characters.
  171.  
  172. `[ LIST ]'
  173.      Match the list of characters specified by LIST.  If the first
  174.      character is `!' or `^', match all characters except those
  175.      specified by LIST.  For example, the pattern `[a-zA-Z]' will match
  176.      all lower and upper case alphabetic characters.
  177.  
  178.    The following options may be specified for `save'.
  179.  
  180. `-ascii'
  181.      Save the data in Octave's text data format.  Using this flag
  182.      overrides the value of the built-in variable `default_save_format'.
  183.  
  184. `-binary'
  185.      Save the data in Octave's binary data format.  Using this flag
  186.      overrides the value of the built-in variable `default_save_format'.
  187.  
  188. `-float-binary'
  189.      Save the data in Octave's binary data format but only using single
  190.      precision.  Using this flag overrides the value of the built-in
  191.      variable `default_save_precision'.  You should use this format
  192.      only if you know that all the values to be saved can be
  193.      represented in single precision.
  194.  
  195. `-mat-binary'
  196.      Save the data in MATLAB's binary data format.  Using this flag
  197.      overrides the value of the built-in variable `default_save_format'.
  198.  
  199. `-save-builtins'
  200.      Force Octave to save the values of built-in variables too.  By
  201.      default, Octave does not save built-in variables.
  202.  
  203.    Saving global variables also saves the global status of the
  204. variable, so that if it is restored at a later time using `load', it
  205. will be restored as a global variable.
  206.  
  207.    To restore the values from a file, use the `load' command.  For
  208. example, to restore the variables saved in the file `data', use the
  209. command
  210.  
  211.      load data
  212.  
  213.    Octave will refuse to overwrite existing variables unless you use the
  214. option `-force'.
  215.  
  216.    If a variable that is not marked as global is loaded from a file
  217. when a global symbol with the same name already exists, it is loaded in
  218. the global symbol table.  Also, if a variable is marked as global in a
  219. file and a local symbol exists, the local symbol is moved to the global
  220. symbol table and given the value from the file.  Since it seems that
  221. both of these cases are likely to be the result of some sort of error,
  222. they will generate warnings.
  223.  
  224.    As with `save', you may specify a list of variables and `load' will
  225. only extract those variables with names that match.
  226.  
  227.    The `load' command can read data stored in Octave's text and binary
  228. formats, and MATLAB's binary format.  It will automatically detect the
  229. type of file and do conversion from different floating point formats
  230. (currently only IEEE big and little endian, though other formats may
  231. added in the future).
  232.  
  233.    The following options may be specified for `save'.
  234.  
  235. `-force'
  236.      Force variables currently in memory to be overwritten by variables
  237.      with the same name found in the file.
  238.  
  239. `-ascii'
  240.      Force Octave to assume the file is in Octave's text format.
  241.  
  242. `-binary'
  243.      Force Octave to assume the file is in Octave's binary format.
  244.  
  245. `-mat-binary'
  246.      Force Octave to assume the file is in MATLAB's binary format.
  247.  
  248. 
  249. File: octave.info,  Node: C-Style I/O Functions,  Prev: Basic Input and Output,  Up: Input and Output
  250.  
  251. C-Style I/O Functions
  252. =====================
  253.  
  254.    The C-style input and output functions provide most of the
  255. functionality of the C programming language's standard I/O library.
  256. The argument lists for some of the input functions are slightly
  257. different, however, because Octave has no way of passing arguments by
  258. reference.
  259.  
  260.    In the following, FILE refers either to an integer file number (as
  261. returned by `fopen') or a file name.
  262.  
  263.    There are three files that are always available:
  264.  
  265. `stdin'
  266.      The standard input stream (file number 0).  When Octave is used
  267.      interactively, this is filtered through the command line editing
  268.      functions.
  269.  
  270. `stdout'
  271.      The standard output stream (file number 1).  Data written to the
  272.      standard output is normally filtered through the pager.
  273.  
  274. `stderr'
  275.      The standard error stream (file number 2).  Even if paging is
  276.      turned on, the standard error is not sent to the pager.  It is
  277.      useful for error messages and prompts.
  278.  
  279.    You should always use the symbolic names given in the table above,
  280. rather than referring to these files by number, since it will make your
  281. programs clearer.
  282.  
  283. * Menu:
  284.  
  285. * Opening and Closing Files::
  286. * Formatted Output::
  287. * Output Conversion Syntax::
  288. * Table of Output Conversions::
  289. * Integer Conversions::
  290. * Floating-Point Conversions::
  291. * Other Output Conversions::
  292. * Formatted Input::
  293. * Input Conversion Syntax::
  294. * Table of Input Conversions::
  295. * Numeric Input Conversions::
  296. * String Input Conversions::
  297. * Binary I/O::
  298. * Other I/O Functions::
  299.  
  300. 
  301. File: octave.info,  Node: Opening and Closing Files,  Next: Formatted Output,  Prev: C-Style I/O Functions,  Up: C-Style I/O Functions
  302.  
  303. Opening and Closing Files
  304. -------------------------
  305.  
  306.    To open a file, use the function `fopen (name, mode)'.  It returns
  307. an integer value that may be used to refer to the file later.  The
  308. second argument is a one or two character string that specifies whether
  309. the file is to be opened for reading, writing, or both.
  310.  
  311.    For example,
  312.  
  313.      myfile = fopen ("splat.dat", "r");
  314.  
  315. opens the file `splat.dat' for reading.  Opening a file that is already
  316. open has no effect.
  317.  
  318.    The possible values `mode' may have are
  319.  
  320. `r'
  321.      Open a file for reading.
  322.  
  323. `w'
  324.      Open a file for writing.  The previous contents are discared.
  325.  
  326. `a'
  327.      Open or create a file for writing at the end of the file.
  328.  
  329. `r+'
  330.      Open an existing file for reading and writing.
  331.  
  332. `w+'
  333.      Open a file for reading or writing.  The previous contents are
  334.      discared.
  335.  
  336. `a+'
  337.      Open or create a file for reading or writing at the end of the
  338.      file.
  339.  
  340.    To close a file once you are finished with it, use the function
  341. `fclose (FILE)'.  If an error is encountered while trying to close the
  342. file, an error message is printed and `fclose' returns 0.  Otherwise,
  343. it returns 1.
  344.  
  345. 
  346. File: octave.info,  Node: Formatted Output,  Next: Output Conversion Syntax,  Prev: Opening and Closing Files,  Up: C-Style I/O Functions
  347.  
  348. Formatted Output
  349. ----------------
  350.  
  351.    This section describes how to call `printf' and related functions.
  352.  
  353.    The following functions are available for formatted output.  They are
  354. modelled after the C language functions of the same name.
  355.  
  356. `printf (TEMPLATE, ...)'
  357.      The `printf' function prints the optional arguments under the
  358.      control of the template string TEMPLATE to the stream `stdout'.
  359.  
  360. `fprintf (FILE, TEMPLATE, ...)'
  361.      This function is just like `printf', except that the output is
  362.      written to the stream FILE instead of `stdout'.
  363.  
  364. `sprintf (TEMPLATE, ...)'
  365.      This is like `printf', except that the output is written to a
  366.      string.  Unlike the C library function, which requires you to
  367.      provide a suitably sized string as an argument, Octave's `sprintf'
  368.      function returns the string, automatically sized to hold all of
  369.      the items converted.
  370.  
  371.    The `printf' function can be used to print any number of arguments.
  372. The template string argument you supply in a call provides information
  373. not only about the number of additional arguments, but also about their
  374. types and what style should be used for printing them.
  375.  
  376.    Ordinary characters in the template string are simply written to the
  377. output stream as-is, while "conversion specifications" introduced by a
  378. `%' character in the template cause subsequent arguments to be
  379. formatted and written to the output stream.  For example,
  380.  
  381.      pct = 37;
  382.      filename = "foo.txt";
  383.      printf ("Processing of `%s' is %d%% finished.\nPlease be patient.\n",
  384.              filename, pct);
  385.  
  386. produces output like
  387.  
  388.      Processing of `foo.txt' is 37% finished.
  389.      Please be patient.
  390.  
  391.    This example shows the use of the `%d' conversion to specify that a
  392. scalar argument should be printed in decimal notation, the `%s'
  393. conversion to specify printing of a string argument, and the `%%'
  394. conversion to print a literal `%' character.
  395.  
  396.    There are also conversions for printing an integer argument as an
  397. unsigned value in octal, decimal, or hexadecimal radix (`%o', `%u', or
  398. `%x', respectively); or as a character value (`%c').
  399.  
  400.    Floating-point numbers can be printed in normal, fixed-point notation
  401. using the `%f' conversion or in exponential notation using the `%e'
  402. conversion.  The `%g' conversion uses either `%e' or `%f' format,
  403. depending on what is more appropriate for the magnitude of the
  404. particular number.
  405.  
  406.    You can control formatting more precisely by writing "modifiers"
  407. between the `%' and the character that indicates which conversion to
  408. apply.  These slightly alter the ordinary behavior of the conversion.
  409. For example, most conversion specifications permit you to specify a
  410. minimum field width and a flag indicating whether you want the result
  411. left- or right-justified within the field.
  412.  
  413.    The specific flags and modifiers that are permitted and their
  414. interpretation vary depending on the particular conversion.  They're all
  415. described in more detail in the following sections.
  416.  
  417. 
  418. File: octave.info,  Node: Output Conversion Syntax,  Next: Table of Output Conversions,  Prev: Formatted Output,  Up: C-Style I/O Functions
  419.  
  420. Output Conversion Syntax
  421. ------------------------
  422.  
  423.    This section provides details about the precise syntax of conversion
  424. specifications that can appear in a `printf' template string.
  425.  
  426.    Characters in the template string that are not part of a conversion
  427. specification are printed as-is to the output stream.
  428.  
  429.    The conversion specifications in a `printf' template string have the
  430. general form:
  431.  
  432.      % FLAGS WIDTH [ . PRECISION ] TYPE CONVERSION
  433.  
  434.    For example, in the conversion specifier `%-10.8ld', the `-' is a
  435. flag, `10' specifies the field width, the precision is `8', the letter
  436. `l' is a type modifier, and `d' specifies the conversion style.  (This
  437. particular type specifier says to print a numeric argument in decimal
  438. notation, with a minimum of 8 digits left-justified in a field at least
  439. 10 characters wide.)
  440.  
  441.    In more detail, output conversion specifications consist of an
  442. initial `%' character followed in sequence by:
  443.  
  444.    * Zero or more "flag characters" that modify the normal behavior of
  445.      the conversion specification.
  446.  
  447.    * An optional decimal integer specifying the "minimum field width".
  448.      If the normal conversion produces fewer characters than this, the
  449.      field is padded with spaces to the specified width.  This is a
  450.      *minimum* value; if the normal conversion produces more characters
  451.      than this, the field is *not* truncated.  Normally, the output is
  452.      right-justified within the field.
  453.  
  454.      You can also specify a field width of `*'.  This means that the
  455.      next argument in the argument list (before the actual value to be
  456.      printed) is used as the field width.  The value is rounded to the
  457.      nearest integer.  If the value is negative, this means to set the
  458.      `-' flag (see below) and to use the absolute value as the field
  459.      width.
  460.  
  461.    * An optional "precision" to specify the number of digits to be
  462.      written for the numeric conversions.  If the precision is
  463.      specified, it consists of a period (`.') followed optionally by a
  464.      decimal integer (which defaults to zero if omitted).
  465.  
  466.      You can also specify a precision of `*'.  This means that the next
  467.      argument in the argument list (before the actual value to be
  468.      printed) is used as the precision.  The value must be an integer,
  469.      and is ignored if it is negative.
  470.  
  471.    * An optional "type modifier character".  This character is ignored
  472.      by Octave's `printf' function, but is recognized to provide
  473.      compatibility with the C language `printf'.
  474.  
  475.    * A character that specifies the conversion to be applied.
  476.  
  477.    The exact options that are permitted and how they are interpreted
  478. vary between the different conversion specifiers.  See the descriptions
  479. of the individual conversions for information about the particular
  480. options that they use.
  481.  
  482. 
  483. File: octave.info,  Node: Table of Output Conversions,  Next: Integer Conversions,  Prev: Output Conversion Syntax,  Up: C-Style I/O Functions
  484.  
  485. Table of Output Conversions
  486. ---------------------------
  487.  
  488.    Here is a table summarizing what all the different conversions do:
  489.  
  490. `%d', `%i'
  491.      Print an integer as a signed decimal number.  *Note Integer
  492.      Conversions::, for details.  `%d' and `%i' are synonymous for
  493.      output, but are different when used with `scanf' for input (*note
  494.      Table of Input Conversions::.).
  495.  
  496. `%o'
  497.      Print an integer as an unsigned octal number.  *Note Integer
  498.      Conversions::, for details.
  499.  
  500. `%u'
  501.      Print an integer as an unsigned decimal number.  *Note Integer
  502.      Conversions::, for details.
  503.  
  504. `%x', `%X'
  505.      Print an integer as an unsigned hexadecimal number.  `%x' uses
  506.      lower-case letters and `%X' uses upper-case.  *Note Integer
  507.      Conversions::, for details.
  508.  
  509. `%f'
  510.      Print a floating-point number in normal (fixed-point) notation.
  511.      *Note Floating-Point Conversions::, for details.
  512.  
  513. `%e', `%E'
  514.      Print a floating-point number in exponential notation.  `%e' uses
  515.      lower-case letters and `%E' uses upper-case.  *Note Floating-Point
  516.      Conversions::, for details.
  517.  
  518. `%g', `%G'
  519.      Print a floating-point number in either normal or exponential
  520.      notation, whichever is more appropriate for its magnitude.  `%g'
  521.      uses lower-case letters and `%G' uses upper-case.  *Note
  522.      Floating-Point Conversions::, for details.
  523.  
  524. `%c'
  525.      Print a single character.  *Note Other Output Conversions::.
  526.  
  527. `%s'
  528.      Print a string.  *Note Other Output Conversions::.
  529.  
  530. `%%'
  531.      Print a literal `%' character.  *Note Other Output Conversions::.
  532.  
  533.    If the syntax of a conversion specification is invalid, unpredictable
  534. things will happen, so don't do this.  If there aren't enough function
  535. arguments provided to supply values for all the conversion
  536. specifications in the template string, or if the arguments are not of
  537. the correct types, the results are unpredictable.  If you supply more
  538. arguments than conversion specifications, the extra argument values are
  539. simply ignored; this is sometimes useful.
  540.  
  541. 
  542. File: octave.info,  Node: Integer Conversions,  Next: Floating-Point Conversions,  Prev: Table of Output Conversions,  Up: C-Style I/O Functions
  543.  
  544. Integer Conversions
  545. -------------------
  546.  
  547.    This section describes the options for the `%d', `%i', `%o', `%u',
  548. `%x', and `%X' conversion specifications.  These conversions print
  549. integers in various formats.
  550.  
  551.    The `%d' and `%i' conversion specifications both print an numeric
  552. argument as a signed decimal number; while `%o', `%u', and `%x' print
  553. the argument as an unsigned octal, decimal, or hexadecimal number
  554. (respectively).  The `%X' conversion specification is just like `%x'
  555. except that it uses the characters `ABCDEF' as digits instead of
  556. `abcdef'.
  557.  
  558.    The following flags are meaningful:
  559.  
  560. `-'
  561.      Left-justify the result in the field (instead of the normal
  562.      right-justification).
  563.  
  564. `+'
  565.      For the signed `%d' and `%i' conversions, print a plus sign if the
  566.      value is positive.
  567.  
  568. ` '
  569.      For the signed `%d' and `%i' conversions, if the result doesn't
  570.      start with a plus or minus sign, prefix it with a space character
  571.      instead.  Since the `+' flag ensures that the result includes a
  572.      sign, this flag is ignored if you supply both of them.
  573.  
  574. `#'
  575.      For the `%o' conversion, this forces the leading digit to be `0',
  576.      as if by increasing the precision.  For `%x' or `%X', this
  577.      prefixes a leading `0x' or `0X' (respectively) to the result.
  578.      This doesn't do anything useful for the `%d', `%i', or `%u'
  579.      conversions.
  580.  
  581. `0'
  582.      Pad the field with zeros instead of spaces.  The zeros are placed
  583.      after any indication of sign or base.  This flag is ignored if the
  584.      `-' flag is also specified, or if a precision is specified.
  585.  
  586.    If a precision is supplied, it specifies the minimum number of
  587. digits to appear; leading zeros are produced if necessary.  If you
  588. don't specify a precision, the number is printed with as many digits as
  589. it needs.  If you convert a value of zero with an explicit precision of
  590. zero, then no characters at all are produced.
  591.  
  592. 
  593. File: octave.info,  Node: Floating-Point Conversions,  Next: Other Output Conversions,  Prev: Integer Conversions,  Up: C-Style I/O Functions
  594.  
  595. Floating-Point Conversions
  596. --------------------------
  597.  
  598.    This section discusses the conversion specifications for
  599. floating-point numbers: the `%f', `%e', `%E', `%g', and `%G'
  600. conversions.
  601.  
  602.    The `%f' conversion prints its argument in fixed-point notation,
  603. producing output of the form [`-']DDD`.'DDD, where the number of digits
  604. following the decimal point is controlled by the precision you specify.
  605.  
  606.    The `%e' conversion prints its argument in exponential notation,
  607. producing output of the form [`-']D`.'DDD`e'[`+'|`-']DD.  Again, the
  608. number of digits following the decimal point is controlled by the
  609. precision.  The exponent always contains at least two digits.  The `%E'
  610. conversion is similar but the exponent is marked with the letter `E'
  611. instead of `e'.
  612.  
  613.    The `%g' and `%G' conversions print the argument in the style of
  614. `%e' or `%E' (respectively) if the exponent would be less than -4 or
  615. greater than or equal to the precision; otherwise they use the `%f'
  616. style.  Trailing zeros are removed from the fractional portion of the
  617. result and a decimal-point character appears only if it is followed by
  618. a digit.
  619.  
  620.    The following flags can be used to modify the behavior:
  621.  
  622. `-'
  623.      Left-justify the result in the field.  Normally the result is
  624.      right-justified.
  625.  
  626. `+'
  627.      Always include a plus or minus sign in the result.
  628.  
  629. ` '
  630.      If the result doesn't start with a plus or minus sign, prefix it
  631.      with a space instead.  Since the `+' flag ensures that the result
  632.      includes a sign, this flag is ignored if you supply both of them.
  633.  
  634. `#'
  635.      Specifies that the result should always include a decimal point,
  636.      even if no digits follow it.  For the `%g' and `%G' conversions,
  637.      this also forces trailing zeros after the decimal point to be left
  638.      in place where they would otherwise be removed.
  639.  
  640. `0'
  641.      Pad the field with zeros instead of spaces; the zeros are placed
  642.      after any sign.  This flag is ignored if the `-' flag is also
  643.      specified.
  644.  
  645.    The precision specifies how many digits follow the decimal-point
  646. character for the `%f', `%e', and `%E' conversions.  For these
  647. conversions, the default precision is `6'.  If the precision is
  648. explicitly `0', this suppresses the decimal point character entirely.
  649. For the `%g' and `%G' conversions, the precision specifies how many
  650. significant digits to print.  Significant digits are the first digit
  651. before the decimal point, and all the digits after it.  If the
  652. precision is `0' or not specified for `%g' or `%G', it is treated like
  653. a value of `1'.  If the value being printed cannot be expressed
  654. precisely in the specified number of digits, the value is rounded to
  655. the nearest number that fits.
  656.  
  657. 
  658. File: octave.info,  Node: Other Output Conversions,  Next: Formatted Input,  Prev: Floating-Point Conversions,  Up: C-Style I/O Functions
  659.  
  660. Other Output Conversions
  661. ------------------------
  662.  
  663.    This section describes miscellaneous conversions for `printf'.
  664.  
  665.    The `%c' conversion prints a single character.  The `-' flag can be
  666. used to specify left-justification in the field, but no other flags are
  667. defined, and no precision or type modifier can be given.  For example:
  668.  
  669.      printf ("%c%c%c%c%c", "h", "e", "l", "l", "o");
  670.  
  671. prints `hello'.
  672.  
  673.    The `%s' conversion prints a string.  The corresponding argument
  674. must be a string.  A precision can be specified to indicate the maximum
  675. number of characters to write; otherwise characters in the string up to
  676. but not including the terminating null character are written to the
  677. output stream.  The `-' flag can be used to specify left-justification
  678. in the field, but no other flags or type modifiers are defined for this
  679. conversion.  For example:
  680.  
  681.      printf ("%3s%-6s", "no", "where");
  682.  
  683. prints ` nowhere '.
  684.  
  685. 
  686. File: octave.info,  Node: Formatted Input,  Next: Input Conversion Syntax,  Prev: Other Output Conversions,  Up: C-Style I/O Functions
  687.  
  688. Formatted Input
  689. ---------------
  690.  
  691.    Here are the descriptions of the functions for performing formatted
  692. input.
  693.  
  694. `scanf (TEMPLATE)'
  695.      The `scanf' function reads formatted input from the stream `stdin'
  696.      under the control of the template string TEMPLATE.  The resulting
  697.      values are returned.
  698.  
  699. `fscanf (FILE, TEMPLATE)'
  700.      This function is just like `scanf', except that the input is read
  701.      from the stream FILE instead of `stdin'.
  702.  
  703. `sscanf (STRING, TEMPLATE)'
  704.      This is like `scanf', except that the characters are taken from the
  705.      string STRING instead of from a stream.  Reaching the end of the
  706.      string is treated as an end-of-file condition.
  707.  
  708.    Calls to `scanf' are superficially similar to calls to `printf' in
  709. that arbitrary arguments are read under the control of a template
  710. string.  While the syntax of the conversion specifications in the
  711. template is very similar to that for `printf', the interpretation of
  712. the template is oriented more towards free-format input and simple
  713. pattern matching, rather than fixed-field formatting.  For example,
  714. most `scanf' conversions skip over any amount of "white space"
  715. (including spaces, tabs, and newlines) in the input file, and there is
  716. no concept of precision for the numeric input conversions as there is
  717. for the corresponding output conversions.  Ordinarily, non-whitespace
  718. characters in the template are expected to match characters in the
  719. input stream exactly.
  720.  
  721.    When a "matching failure" occurs, `scanf' returns immediately,
  722. leaving the first non-matching character as the next character to be
  723. read from the stream, and `scanf' returns all the items that were
  724. successfully converted.
  725.  
  726.    The formatted input functions are not used as frequently as the
  727. formatted output functions.  Partly, this is because it takes some care
  728. to use them properly.  Another reason is that it is difficult to recover
  729. from a matching error.
  730.  
  731. 
  732. File: octave.info,  Node: Input Conversion Syntax,  Next: Table of Input Conversions,  Prev: Formatted Input,  Up: C-Style I/O Functions
  733.  
  734. Input Conversion Syntax
  735. -----------------------
  736.  
  737.    A `scanf' template string is a string that contains ordinary
  738. multibyte characters interspersed with conversion specifications that
  739. start with `%'.
  740.  
  741.    Any whitespace character in the template causes any number of
  742. whitespace characters in the input stream to be read and discarded.
  743. The whitespace characters that are matched need not be exactly the same
  744. whitespace characters that appear in the template string.  For example,
  745. write ` , ' in the template to recognize a comma with optional
  746. whitespace before and after.
  747.  
  748.    Other characters in the template string that are not part of
  749. conversion specifications must match characters in the input stream
  750. exactly; if this is not the case, a matching failure occurs.
  751.  
  752.    The conversion specifications in a `scanf' template string have the
  753. general form:
  754.  
  755.      % FLAGS WIDTH TYPE CONVERSION
  756.  
  757.    In more detail, an input conversion specification consists of an
  758. initial `%' character followed in sequence by:
  759.  
  760.    * An optional "flag character" `*', which says to ignore the text
  761.      read for this specification.  When `scanf' finds a conversion
  762.      specification that uses this flag, it reads input as directed by
  763.      the rest of the conversion specification, but it discards this
  764.      input, does not use a pointer argument, and does not increment the
  765.      count of successful assignments.
  766.  
  767.    * An optional decimal integer that specifies the "maximum field
  768.      width".  Reading of characters from the input stream stops either
  769.      when this maximum is reached or when a non-matching character is
  770.      found, whichever happens first.  Most conversions discard initial
  771.      whitespace characters (those that don't are explicitly
  772.      documented), and these discarded characters don't count towards
  773.      the maximum field width.
  774.  
  775.    * An optional type modifier character.  This character is ignored by
  776.      Octave's `scanf' function, but is recognized to provide
  777.      compatibility with the C language `scanf'.
  778.  
  779.    * A character that specifies the conversion to be applied.
  780.  
  781.    The exact options that are permitted and how they are interpreted
  782. vary between the different conversion specifiers.  See the descriptions
  783. of the individual conversions for information about the particular
  784. options that they allow.
  785.  
  786. 
  787. File: octave.info,  Node: Table of Input Conversions,  Next: Numeric Input Conversions,  Prev: Input Conversion Syntax,  Up: C-Style I/O Functions
  788.  
  789. Table of Input Conversions
  790. --------------------------
  791.  
  792.    Here is a table that summarizes the various conversion
  793. specifications:
  794.  
  795. `%d'
  796.      Matches an optionally signed integer written in decimal.  *Note
  797.      Numeric Input Conversions::.
  798.  
  799. `%i'
  800.      Matches an optionally signed integer in any of the formats that
  801.      the C language defines for specifying an integer constant.  *Note
  802.      Numeric Input Conversions::.
  803.  
  804. `%o'
  805.      Matches an unsigned integer written in octal radix.  *Note Numeric
  806.      Input Conversions::.
  807.  
  808. `%u'
  809.      Matches an unsigned integer written in decimal radix.  *Note
  810.      Numeric Input Conversions::.
  811.  
  812. `%x', `%X'
  813.      Matches an unsigned integer written in hexadecimal radix.  *Note
  814.      Numeric Input Conversions::.
  815.  
  816. `%e', `%f', `%g', `%E', `%G'
  817.      Matches an optionally signed floating-point number.  *Note Numeric
  818.      Input Conversions::.
  819.  
  820. `%s'
  821.      Matches a string containing only non-whitespace characters.  *Note
  822.      String Input Conversions::.
  823.  
  824. `%c'
  825.      Matches a string of one or more characters; the number of
  826.      characters read is controlled by the maximum field width given for
  827.      the conversion.  *Note String Input Conversions::.
  828.  
  829. `%%'
  830.      This matches a literal `%' character in the input stream.  No
  831.      corresponding argument is used.
  832.  
  833.    If the syntax of a conversion specification is invalid, the behavior
  834. is undefined.  If there aren't enough function arguments provided to
  835. supply addresses for all the conversion specifications in the template
  836. strings that perform assignments, or if the arguments are not of the
  837. correct types, the behavior is also undefined.  On the other hand, extra
  838. arguments are simply ignored.
  839.  
  840. 
  841. File: octave.info,  Node: Numeric Input Conversions,  Next: String Input Conversions,  Prev: Table of Input Conversions,  Up: C-Style I/O Functions
  842.  
  843. Numeric Input Conversions
  844. -------------------------
  845.  
  846.    This section describes the `scanf' conversions for reading numeric
  847. values.
  848.  
  849.    The `%d' conversion matches an optionally signed integer in decimal
  850. radix.
  851.  
  852.    The `%i' conversion matches an optionally signed integer in any of
  853. the formats that the C language defines for specifying an integer
  854. constant.
  855.  
  856.    For example, any of the strings `10', `0xa', or `012' could be read
  857. in as integers under the `%i' conversion.  Each of these specifies a
  858. number with decimal value `10'.
  859.  
  860.    The `%o', `%u', and `%x' conversions match unsigned integers in
  861. octal, decimal, and hexadecimal radices, respectively.
  862.  
  863.    The `%X' conversion is identical to the `%x' conversion.  They both
  864. permit either uppercase or lowercase letters to be used as digits.
  865.  
  866.    Unlike the C language `scanf', Octave ignores the `h', `l', and `L'
  867. modifiers.
  868.  
  869. 
  870. File: octave.info,  Node: String Input Conversions,  Next: Binary I/O,  Prev: Numeric Input Conversions,  Up: C-Style I/O Functions
  871.  
  872. String Input Conversions
  873. ------------------------
  874.  
  875.    This section describes the `scanf' input conversions for reading
  876. string and character values: `%s' and `%c'.
  877.  
  878.    The `%c' conversion is the simplest: it matches a fixed number of
  879. characters, always.  The maximum field with says how many characters to
  880. read; if you don't specify the maximum, the default is 1.  This
  881. conversion does not skip over initial whitespace characters.  It reads
  882. precisely the next N characters, and fails if it cannot get that many.
  883.  
  884.    The `%s' conversion matches a string of non-whitespace characters.
  885. It skips and discards initial whitespace, but stops when it encounters
  886. more whitespace after having read something.
  887.  
  888.    For example, reading the input:
  889.  
  890.       hello, world
  891.  
  892. with the conversion `%10c' produces `" hello, wo"', but reading the
  893. same input with the conversion `%10s' produces `"hello,"'.
  894.  
  895. 
  896. File: octave.info,  Node: Binary I/O,  Next: Other I/O Functions,  Prev: String Input Conversions,  Up: C-Style I/O Functions
  897.  
  898. Binary I/O
  899. ----------
  900.  
  901.    Octave has to C-style functions for reading and writing binary data.
  902. They are `fread' and `fwrite' and are patterned after the standard C
  903. functions with the same names.
  904.  
  905. `fread (FILE, SIZE, PRECISION)'
  906.      This function reads data in binary form of type PRECISION from the
  907.      specified FILE, which may be either a file name, or a file number
  908.      as returned from `fopen'.
  909.  
  910.      The argument SIZE specifies the size of the matrix to return.  It
  911.      may be a scalar or a two-element vector.  If it is a scalar,
  912.      `fread' returns a column vector of the specified length.  If it is
  913.      a two-element vector, it specifies the number of rows and columns
  914.      of the result matrix, and `fread' fills the elements of the matrix
  915.      in column-major order.
  916.  
  917.      The argument PRECISION is a string specifying the type of data to
  918.      read and may be one of `"char"', `"schar"', `"short"', `"int"',
  919.      `"long"', `"float"', `"double"', `"uchar"', `"ushort"', `"uint"',
  920.      or `"ulong"'.  The default precision is `"uchar"'.
  921.  
  922.      The `fread' function returns two values, `data', which is the data
  923.      read from the file, and `count', which is the number of elements
  924.      read.
  925.  
  926. `fwrite (FILE, DATA, PRECISION)'
  927.      This function writes data in binary form of type PRECISION to the
  928.      specified FILE, which may be either a file name, or a file number
  929.      as returned from `fopen'.
  930.  
  931.      The argument DATA is a matrix of values that are to be written to
  932.      the file.  The values are extracted in column-major order.
  933.  
  934.      The argument PRECISION is a string specifying the type of data to
  935.      read and may be one of `"char"', `"schar"', `"short"', `"int"',
  936.      `"long"', `"float"', `"double"', `"uchar"', `"ushort"', `"uint"',
  937.      or `"ulong"'.  The default precision is `"uchar"'.
  938.  
  939.      The `fwrite' function returns the number of elements written.
  940.  
  941.      The behavior of `fwrite' is undefined if the values in DATA are
  942.      too large to fit in the specified precision.
  943.  
  944. 
  945. File: octave.info,  Node: Other I/O Functions,  Prev: Binary I/O,  Up: C-Style I/O Functions
  946.  
  947. Other I/O Functions
  948. -------------------
  949.  
  950.      fgets (FILE, len)
  951.    Read `len' characters from a file.
  952.  
  953.    To flush output to a stream, use the function `fflush (FILE)'.  This
  954. is useful for ensuring that all pending output makes it to the screen
  955. before some other event occurs.  For example, it is always a good idea
  956. to flush the standard output stream before calling `input'.
  957.  
  958.    Three functions are available for setting and determining the
  959. position of the file pointer for a given file.
  960.  
  961.    The position of the file pointer (as the number of characters from
  962. the beginning of the file) can be obtained using the the function
  963. `ftell (FILE)'.
  964.  
  965.    To set the file pointer to any location within the file, use the
  966. function `fseek (FILE, offset, origin)'.  The pointer is placed
  967. `offset' characters from the `origin', which may be one of the
  968. predefined variables `SEEK_CUR' (current position), `SEEK_SET'
  969. (beginning), or `SEEK_END' (end of file). If `origin' is omitted,
  970. `SEEK_SET' is assumed.  The offset must be zero, or a value returned by
  971. `ftell' (in which case `origin' must be `SEEK_SET'.  *Note Predefined
  972. Constants::.
  973.  
  974.    The function `frewind (FILE)' moves the file pointer to the
  975. beginning of a file, returning 1 for success, and 0 if an error was
  976. encountered.  It is equivalent to `fseek (FILE, 0, SEEK_SET)'.
  977.  
  978.    The following example stores the current file position in the
  979. variable `marker', moves the pointer to the beginning of the file, reads
  980. four characters, and then returns to the original position.
  981.  
  982.      marker = ftell (myfile);
  983.      frewind (myfile);
  984.      fourch = fgets (myfile, 4);
  985.      fseek (myfile, marker, SEEK_SET);
  986.  
  987.    The function `feof (FILE)' allows you to find out if an end-of-file
  988. condition has been encountered for a given file.  Note that it will
  989. only return 1 if the end of the file has already been encountered, not
  990. if the next read operation will result in an end-of-file condition.
  991.  
  992.    Similarly, the function `ferror (FILE)' allows you to find out if an
  993. error condition has been encountered for a given file.  Note that it
  994. will only return 1 if an error has already been encountered, not if the
  995. next operation will result in an error condition.
  996.  
  997.    The function `kbhit' may be usd to read a single keystroke from the
  998. keyboard.  For example,
  999.  
  1000.      x = kbhit ();
  1001.  
  1002. will set X to the next character typed at the keyboard, without
  1003. requiring a carriage return to be typed.
  1004.  
  1005.    Finally, it is often useful to know exactly which files have been
  1006. opened, and whether they are open for reading, writing, or both.  The
  1007. command `freport' prints this information for all open files.  For
  1008. example,
  1009.  
  1010.      octave:13> freport
  1011.      
  1012.       number  mode  name
  1013.      
  1014.            0     r  stdin
  1015.            1     w  stdout
  1016.            2     w  stderr
  1017.            3     r  myfile
  1018.  
  1019. 
  1020. File: octave.info,  Node: Special Matrices,  Next: Matrix Manipulation,  Prev: Input and Output,  Up: Top
  1021.  
  1022. Special Matrices
  1023. ****************
  1024.  
  1025.    Octave provides a number of functions for creating special matrix
  1026. forms.  In nearly all cases, it is best to use the built-in functions
  1027. for this purpose than to try to use other tricks to achieve the same
  1028. effect.
  1029.  
  1030. * Menu:
  1031.  
  1032. * Special Utility Matrices::
  1033. * Famous Matrices::
  1034.  
  1035. 
  1036. File: octave.info,  Node: Special Utility Matrices,  Next: Famous Matrices,  Prev: Special Matrices,  Up: Special Matrices
  1037.  
  1038. Special Utility Matrices
  1039. ========================
  1040.  
  1041.    The function `eye' returns an identity matrix.  If invoked with a
  1042. single scalar argument, `eye' returns a square matrix with the
  1043. dimension specified.  If you supply two scalar arguments, `eye' takes
  1044. them to be the number of rows and columns.  If given a matrix or vector
  1045. argument, `eye' returns an identity matrix with the same dimensions as
  1046. the given argument.
  1047.  
  1048.    For example,
  1049.  
  1050.      eye (3)
  1051.  
  1052. creates an identity matrix with three rows and three columns,
  1053.  
  1054.      eye (5, 8)
  1055.  
  1056. creates an identity matrix with five rows and eight columns, and
  1057.  
  1058.      eye ([13, 21; 34, 55])
  1059.  
  1060. creates an identity matrix with two rows and two columns.
  1061.  
  1062.    Normally, `eye' expects any scalar arguments you provide to be real
  1063. and non-negative.  The variables `ok_to_lose_imaginary_part' and
  1064. `treat_neg_dim_as_zero' control the behavior of `eye' for complex and
  1065. negative arguments.  *Note User Preferences::.  Any non-integer
  1066. arguments are rounded to the nearest integer value.
  1067.  
  1068.    There is an ambiguity when these functions are called with a single
  1069. argument.  You may have intended to create a matrix with the same
  1070. dimensions as another variable, but ended up with something quite
  1071. different, because the variable that you used as an argument was a
  1072. scalar instead of a matrix.
  1073.  
  1074.    For example, if you need to create an identity matrix with the same
  1075. dimensions as another variable in your program, it is best to use code
  1076. like this
  1077.  
  1078.      eye (rows (a), columns (a))
  1079.  
  1080. instead of just
  1081.  
  1082.      eye (a)
  1083.  
  1084. unless you know that the variable A will *always* be a matrix.
  1085.  
  1086.    The functions `ones', `zeros', and `rand' all work like `eye',
  1087. except that they fill the resulting matrix with all ones, all zeros, or
  1088. a set of random values.
  1089.  
  1090.    If you need to create a matrix whose values are all the same, you
  1091. should use an expression like
  1092.  
  1093.      val_matrix = val * ones (n, m)
  1094.  
  1095.    The `rand' function also takes some additional arguments that allow
  1096. you to control its behavior.  For example, the function call
  1097.  
  1098.      rand ("normal")
  1099.  
  1100. causes the sequence of numbers to be normally distributed.  You may also
  1101. use an argument of `"uniform"' to select a uniform distribution.  To
  1102. find out what the current distribution is, use an argument of `"dist"'.
  1103.  
  1104.    Normally, `rand' obtains the seed from the system clock, so that the
  1105. sequence of random numbers is not the same each time you run Octave.
  1106. If you really do need for to reproduce a sequence of numbers exactly,
  1107. you can set the seed to a specific value.  For example, the function
  1108. call
  1109.  
  1110.      rand ("seed", 13)
  1111.  
  1112. sets the seed to the number 13.  To see what the current seed is, use
  1113. the argument `"seed"'.
  1114.  
  1115.    If it is invoked without arguments, `rand' returns a single element
  1116. of a random sequence.
  1117.  
  1118.    The `rand' function uses Fortran code from RANLIB, a library of
  1119. fortran routines for random number generation, compiled by Barry W.
  1120. Brown and James Lovato of the Department of Biomathematics at The
  1121. University of Texas, M.D. Anderson Cancer Center, Houston, TX 77030.
  1122.  
  1123.    To create a diagonal matrix with vector V on diagonal K, use the
  1124. function diag (V, K).  The second argument is optional.  If it is
  1125. positive, the vector is placed on the K-th super-diagonal.  If it is
  1126. negative, it is placed on the -K-th sub-diagonal.  The default value of
  1127. K is 0, and the vector is placed on the main diagonal.  For example,
  1128.  
  1129.      octave:13> diag ([1, 2, 3], 1)
  1130.      ans =
  1131.      
  1132.        0  1  0  0
  1133.        0  0  2  0
  1134.        0  0  0  3
  1135.        0  0  0  0
  1136.  
  1137.    The functions `linspace' and `logspace' make it very easy to create
  1138. vectors with evenly or logarithmically spaced elements.  For example,
  1139.  
  1140.      linspace (BASE, LIMIT, N)
  1141.  
  1142. creates a vector with N (N greater than 2) linearly spaced elements
  1143. between BASE and LIMIT.  The BASE and LIMIT are always included in the
  1144. range.  If BASE is greater than LIMIT, the elements are stored in
  1145. decreasing order.  If the number of points is not specified, a value of
  1146. 100 is used.
  1147.  
  1148.    The function `logspace' is similar to `linspace' except that the
  1149. values are logarithmically spaced.
  1150.  
  1151.    If LIMIT is equal to pi, the points are between 10^base and pi, *not*
  1152. 10^base and 10^pi, in order to  be compatible with the corresponding
  1153. MATLAB function.
  1154.  
  1155. 
  1156. File: octave.info,  Node: Famous Matrices,  Prev: Special Utility Matrices,  Up: Special Matrices
  1157.  
  1158. Famous Matrices
  1159. ===============
  1160.  
  1161.    The following functions return famous matrix forms.
  1162.  
  1163. `hadamard (K)'
  1164.      Return the Hadamard matrix of order n = 2^k.
  1165.  
  1166. `hankel (C, R)'
  1167.      Return the Hankel matrix constructed given the first column C, and
  1168.      (optionally) the last row R.  If the last element of C is not the
  1169.      same as the first element of R, the last element of C is used.  If
  1170.      the second argument is omitted, the last row is taken to be the
  1171.      same as the first column.
  1172.  
  1173.      A Hankel matrix formed from an m-vector C, and an n-vector R, has
  1174.      the elements
  1175.  
  1176.           H (i, j) = c (i+j-1),  i+j-1 <= m;
  1177.           H (i, j) = r (i+j-m),  otherwise
  1178.  
  1179. `hilb (N)'
  1180.      Return the Hilbert matrix of order N.  The i, j element of a
  1181.      Hilbert matrix is defined as
  1182.  
  1183.           H (i, j) = 1 / (i + j - 1)
  1184.  
  1185. `invhilb (N)'
  1186.      Return the inverse of a Hilbert matrix of order N.  This is exact.
  1187.      Compare with the numerical calculation of `inverse (hilb (n))',
  1188.      which suffers from the ill-conditioning of the Hilbert matrix, and
  1189.      the finite precision of your computer's floating point arithmetic.
  1190.  
  1191. `toeplitz (C, R)'
  1192.      Return the Toeplitz matrix constructed given the first column C,
  1193.      and (optionally) the first row R.  If the first element of C is
  1194.      not the same as the first element of R, the first element of C is
  1195.      used.  If the second argument is omitted, the first row is taken
  1196.      to be the same as the first column.
  1197.  
  1198.      A square Toeplitz matrix has the form
  1199.  
  1200.           c(0)  r(1)   r(2)  ...  r(n)
  1201.           c(1)  c(0)   r(1)      r(n-1)
  1202.           c(2)  c(1)   c(0)      r(n-2)
  1203.            .                       .
  1204.            .                       .
  1205.            .                       .
  1206.           
  1207.           c(n) c(n-1) c(n-2) ...  c(0)
  1208.  
  1209. `vander (C)'
  1210.      Return the Vandermonde matrix whose next to last column is C.
  1211.  
  1212.      A Vandermonde matrix has the form
  1213.  
  1214.           c(0)^n ... c(0)^2  c(0)  1
  1215.           c(1)^n ... c(1)^2  c(1)  1
  1216.            .           .      .    .
  1217.            .           .      .    .
  1218.            .           .      .    .
  1219.           
  1220.           c(n)^n ... c(n)^2  c(n)  1
  1221.  
  1222. 
  1223. File: octave.info,  Node: Matrix Manipulation,  Next: String Functions,  Prev: Special Matrices,  Up: Top
  1224.  
  1225. Matrix Manipulation
  1226. *******************
  1227.  
  1228.    There are a number of functions available for checking to see if the
  1229. elements of a matrix meet some condition, and for rearranging the
  1230. elements of a matrix.  For example, Octave can easily tell you if all
  1231. the elements of a matrix are finite, or are less than some specified
  1232. value.  Octave can also rotate the elements, extract the upper- or
  1233. lower-triangular parts, or sort the columns of a matrix.
  1234.  
  1235. * Menu:
  1236.  
  1237. * Finding Elements and Checking Conditions::
  1238. * Rearranging Matrices::
  1239.  
  1240. 
  1241. File: octave.info,  Node: Finding Elements and Checking Conditions,  Next: Rearranging Matrices,  Prev: Matrix Manipulation,  Up: Matrix Manipulation
  1242.  
  1243. Finding Elements and Checking Conditions
  1244. ========================================
  1245.  
  1246.    The functions `any' and `all' are useful for determining whether any
  1247. or all of the elements of a matrix satisfy some condition.  The `find'
  1248. function is also useful in determining which elements of a matrix meet
  1249. a specified condition.
  1250.  
  1251.    Given a vector, the function `any' returns 1 if any element of the
  1252. vector is nonzero.
  1253.  
  1254.    For a matrix argument, `any' returns a row vector of ones and zeros
  1255. with each element indicating whether any of the elements of the
  1256. corresponding column of the matrix are nonzero.  For example,
  1257.  
  1258.      octave:13> any (eye (2, 4))
  1259.      ans =
  1260.      
  1261.        1  1  0  0
  1262.  
  1263.    To see if any of the elements of a matrix are nonzero, you can use a
  1264. statement like
  1265.  
  1266.      any (any (a))
  1267.  
  1268.    For a matrix argument, `any' returns a row vector of ones and zeros
  1269. with each element indicating whether any of the elements of the
  1270. corresponding column of the matrix are nonzero.
  1271.  
  1272.    The function `all' behaves like the function `any', except that it
  1273. returns true only if all the elements of a vector, or all the elements
  1274. in a column of a matrix, are nonzero.
  1275.  
  1276.    Since the comparison operators (*note Comparison Ops::.) return
  1277. matrices of ones and zeros, it is easy to test a matrix for many
  1278. things, not just whether the elements are nonzero.  For example,
  1279.  
  1280.      octave:13> all (all (rand (5) < 0.9))
  1281.      ans = 0
  1282.  
  1283. tests a random 5 by 5 matrix to see if all of it's elements are less
  1284. than 0.9.
  1285.  
  1286.    Note that in conditional contexts (like the test clause of `if' and
  1287. `while' statements) Octave treats the test as if you had typed `all
  1288. (all (condition))'.
  1289.  
  1290.    The functions `isinf', `finite', and `isnan' return 1 if their
  1291. arguments are infinite, finite, or not a number, respectively, and
  1292. return 0 otherwise.  For matrix values, they all work on an element by
  1293. element basis.  For example, evaluating the expression
  1294.  
  1295.      isinf ([1, 2; Inf, 4])
  1296.  
  1297. produces the matrix
  1298.  
  1299.      ans =
  1300.      
  1301.        0  0
  1302.        1  0
  1303.  
  1304.    The function `find' returns a vector of indices of nonzero elements
  1305. of a matrix.  To obtain a single index for each matrix element, Octave
  1306. pretends that the columns of a matrix form one long vector (like Fortran
  1307. arrays are stored).  For example,
  1308.  
  1309.      octave:13> find (eye (2))
  1310.      ans =
  1311.      
  1312.        1
  1313.        4
  1314.  
  1315.    If two outputs are requested, `find' returns the row and column
  1316. indices of nonzero elements of a matrix.  For example,
  1317.  
  1318.      octave:13> [i, j] = find (eye (2))
  1319.      i =
  1320.      
  1321.        1
  1322.        2
  1323.      
  1324.      j =
  1325.      
  1326.        1
  1327.        2
  1328.  
  1329.    If three outputs are requested, `find' also returns the nonzero
  1330. values in a vector.
  1331.  
  1332.