FAQ: Difference between revisions

422 bytes added ,  14 June 2012
m
use Codeline template
m (→‎Toolboxes: URL and link to text)
m (use Codeline template)
Line 123: Line 123:
* Other optimisations in matrix operations
* Other optimisations in matrix operations
* bsxfun optimised for basic arithmetic functions
* bsxfun optimised for basic arithmetic functions
* Matlab-style ignoring of output arguments using <tt>~</tt>
* Matlab-style ignoring of output arguments using {{Codeline|~}}
* Many optimisations of the accumarray function
* Many optimisations of the accumarray function
* Sparse matrix indexing has been rewritten for speed
* Sparse matrix indexing has been rewritten for speed
Line 171: Line 171:
The Octave distribution includes a 650+ page manual that is also distributed under the terms of the GNU GPL. It is available on the web at http://www.octave.org/docs.html and you will also find there instructions on how to order a paper version.
The Octave distribution includes a 650+ page manual that is also distributed under the terms of the GNU GPL. It is available on the web at http://www.octave.org/docs.html and you will also find there instructions on how to order a paper version.


The complete text of the Octave manual is also available using the GNU Info system via the GNU Emacs, info, or xinfo programs, or by using the <tt>doc</tt> command to start the GNU info browser directly from the Octave prompt.
The complete text of the Octave manual is also available using the GNU Info system via the GNU Emacs, info, or xinfo programs, or by using the {{Codeline|doc}} command to start the GNU info browser directly from the Octave prompt.


If you have problems using this documentation, or find that some topic is not adequately explained, indexed, or cross-referenced, please report it on http://bugs.octave.org.
If you have problems using this documentation, or find that some topic is not adequately explained, indexed, or cross-referenced, please report it on http://bugs.octave.org.
Line 256: Line 256:
     ans = Hello Moon
     ans = Hello Moon


As a natural extension of this, functions can also be defined in script files (m-files whose first non-comment line isn't <tt>function out = foo (...)</tt>)
As a natural extension of this, functions can also be defined in script files (m-files whose first non-comment line isn't {{Codeline|function out &#61; foo (...)}})


===Comments with #===
===Comments with #===


The pound character, <tt>#</tt>, may be used to start comments, in addition to <tt>%</tt>. See the previous example. The major advantage of this is that as <tt>#</tt> is also a comment character for unix script files, any file that starts with a string like <tt>#! /usr/bin/octave -q</tt> will be treated as an octave script and be executed by octave.
The pound character, {{Codeline|#}}, may be used to start comments, in addition to {{Codeline|%}}. See the previous example. The major advantage of this is that as {{Codeline|#}} is also a comment character for unix script files, any file that starts with a string like {{Codeline|#! /usr/bin/octave -q}} will be treated as an octave script and be executed by octave.


===Strings delimited by double quotes "===
===Strings delimited by double quotes "===


The double quote, <tt>"</tt>, may be used to delimit strings, in addition to the single quote <tt>'</tt>. See the previous example. Also, double-quoted strings include backslash interpretation (like C++, C, and Perl) while single quoted are uninterpreted (like Matlab and Perl).
The double quote, {{Codeline|"}}, may be used to delimit strings, in addition to the single quote {{Codeline|'}}. See the previous example. Also, double-quoted strings include backslash interpretation (like C++, C, and Perl) while single quoted are uninterpreted (like Matlab and Perl).


===Line continuation by backslash===
===Line continuation by backslash===


Lines can be continued with a backslash, <tt>\</tt>, in addition to three points <tt>...</tt>. See the previous example.
Lines can be continued with a backslash, {{Codeline|\}}, in addition to three points {{Codeline|...}}. See the previous example.


===Informative block closing===
===Informative block closing===
Line 283: Line 283:
     ans = 0.70711
     ans = 0.70711


In Matlab, it is for example necessary to assign the intermediate result <tt>cos([0 pi pi/4 7])</tt> to a variable before it can be indexed again.
In Matlab, it is for example necessary to assign the intermediate result {{Codeline|cos([0 pi pi/4 7])}} to a variable before it can be indexed again.


===Exclamation mark as not operator===
===Exclamation mark as not operator===


The exclamation mark <tt>!</tt> (aka “Bang!”) is a negation operator, just like the tilde <tt>~</tt>:
The exclamation mark {{Codeline|!}} (aka “Bang!”) is a negation operator, just like the tilde {{Codeline|~}}:


     octave:1> if ! strcmp (program_name, "octave"),
     octave:1> if ! strcmp (program_name, "octave"),
Line 295: Line 295:
     > end
     > end
     ans = It works!
     ans = It works!
Note however that Matlab uses the <tt>!</tt> operator for shell escapes, for which Octave requires using the system command.
Note however that Matlab uses the {{Codeline|!}} operator for shell escapes, for which Octave requires using the system command.


===Increment and decrement operators===
===Increment and decrement operators===


If you like the <tt>++</tt>, <tt>+=</tt> etc operators, rejoice! Octave includes the C-like increment and decrement operators <tt>++</tt> and <tt>--</tt> in both their prefix and postfix forms, in addition to <tt>+=</tt>, <tt>-=</tt>, <tt>*=</tt>, <tt>/=</tt>, <tt>^=</tt>, <tt>.*=</tt>, <tt>./=</tt>, and <tt>.^=</tt>.
If you like the {{Codeline|++}}, {{Codeline|+&#61;}} etc operators, rejoice! Octave includes the C-like increment and decrement operators {{Codeline|++}} and {{Codeline|--}} in both their prefix and postfix forms, in addition to {{Codeline|+-}}, {{Codeline|-&#61;}}, {{Codeline|*&#61;}}, {{Codeline|/&#61;}}, {{Codeline|^&#61;}}, {{Codeline|.*&#61;}}, {{Codeline|./&#61;}}, and {{Codeline|.^&#61;}}.


For example, to pre-increment the variable x, you would write ++x. This would add one to x and then return the new value of x as the result of the expression. It is exactly the same as the expression x = x + 1.
For example, to pre-increment the variable x, you would write {{Codeline|++x}}. This would add one to x and then return the new value of x as the result of the expression. It is exactly the same as the expression {{Codeline|x &#61; x + 1}}.


To post-increment a variable x, you would write x++. This adds one to the variable x, but returns the value that x had prior to incrementing it. For example, if x is equal to 2, the result of the expression x++ is 2, and the new value of x is 3.
To post-increment a variable x, you would write {{Codeline|x++}}. This adds one to the variable x, but returns the value that x had prior to incrementing it. For example, if x is equal to 2, the result of the expression x++ is 2, and the new value of x is 3.


For matrix and vector arguments, the increment and decrement operators work on each element of the operand.
For matrix and vector arguments, the increment and decrement operators work on each element of the operand.
Line 321: Line 321:
The unwind_protect statement is often used to reliably restore the values of global variables that need to be temporarily changed.
The unwind_protect statement is often used to reliably restore the values of global variables that need to be temporarily changed.


Matlab can be made to do something similar with their <tt>OnCleanUp</tt> function that was introduced in 2008a. Octave also has <tt>onCleanup</tt> since version 3.4.0.
Matlab can be made to do something similar with their {{Codeline|OnCleanUp}} function that was introduced in 2008a. Octave also has {{Codeline|onCleanup}} since version 3.4.0.


===Built-in ODE and DAE solvers===
===Built-in ODE and DAE solvers===
Line 329: Line 329:
==How does Octave solve linear systems?==
==How does Octave solve linear systems?==


In addition to consulting Octave's source for the precise details, you can read the Octave manual for a complete high-level description of the algorithm that Octave uses to decide how to solve a particular linear system, e.g. how the backslash operator <tt>A\x</tt> will be interpreted. Sections [http://www.gnu.org/software/octave/doc/interpreter/Techniques-Used-for-Linear-Algebra.html#Techniques-Used-for-Linear-Algebra Techniques Used for Linear Algebra] and [http://www.gnu.org/software/octave/doc/interpreter/Sparse-Linear-Algebra.html Linear Algebra on Sparse Matrices] from the manual describe this procedure.
In addition to consulting Octave's source for the precise details, you can read the Octave manual for a complete high-level description of the algorithm that Octave uses to decide how to solve a particular linear system, e.g. how the backslash operator {{Codeline|A\x}} will be interpreted. Sections [http://www.gnu.org/software/octave/doc/interpreter/Techniques-Used-for-Linear-Algebra.html#Techniques-Used-for-Linear-Algebra Techniques Used for Linear Algebra] and [http://www.gnu.org/software/octave/doc/interpreter/Sparse-Linear-Algebra.html Linear Algebra on Sparse Matrices] from the manual describe this procedure.


==How can I get involved in Octave development?==
==How can I get involved in Octave development?==
Line 468: Line 468:
If you are running an Octave script that includes a plotting command, the script and Octave may terminate immediately. So the plot window does show up, but immediately closes when Octave finishes execution.
If you are running an Octave script that includes a plotting command, the script and Octave may terminate immediately. So the plot window does show up, but immediately closes when Octave finishes execution.


A common solution is to put a <tt>pause</tt> command at the end of your script.
A common solution is to put a {{Codeline|pause}} command at the end of your script.


==How do I get sound output in Windows?==  
==How do I get sound output in Windows?==  
Line 497: Line 497:


* Debian/Ubuntu<br/>
* Debian/Ubuntu<br/>
   <tt>aptitude install octave-headers</tt>
   aptitude install octave-headers


* Fedora<br/>
* Fedora<br/>
   <tt>yum install octave-devel</tt>
   yum install octave-devel


=Porting programs from Matlab to Octave=
=Porting programs from Matlab to Octave=
Line 589: Line 589:
===Block comments===
===Block comments===


Block comments denoted by <tt>#{</tt> and <tt>#}</tt> markers (or <tt>%{</tt> and <tt>%}</tt>) are supported by Octave with some limitations. The major limitation is that block comments are not supported within [] or {}.
Block comments denoted by {{Codeline|#{}} and {{Codeline|#&#125;}}  markers (or {{Codeline|%{}} and {{Codeline|%&#125;}}) are supported by Octave with some limitations. The major limitation is that block comments are not supported within [] or {}.


===Mat-File format===
===Mat-File format===
Line 607: Line 607:
Octave is a community project and so the toolboxes that exist are donated by those interested in them through [http://octave.sourceforge.net Octave Forge]. These might be lacking in certain functionality relative to the Matlab toolboxes, and might not exactly duplicate the Matlab functionality or interface.
Octave is a community project and so the toolboxes that exist are donated by those interested in them through [http://octave.sourceforge.net Octave Forge]. These might be lacking in certain functionality relative to the Matlab toolboxes, and might not exactly duplicate the Matlab functionality or interface.


===Short-circuit <tt>&</tt> and <tt>|</tt> operators===
===Short-circuit {{Codeline|&}} and {{Codeline|&#124;}} operators===


The <tt>&</tt> and <tt>|</tt> operators in Matlab short-circuit when included in a condition (e.g. an <tt>if</tt> or <tt>while</tt> statement) and not otherwise. In Octave only the <tt>&&</tt> and <tt>||</tt> short circuit. Note that this means that
The {{Codeline|&}} and {{Codeline|&#124;}} operators in Matlab short-circuit when included in a condition (e.g. an {{Codeline|if}} or {{Codeline|while}} statement) and not otherwise. In Octave only the {{Codeline|&&}} and {{Codeline|&#124;&#124;}} short circuit. Note that this means that


             if (a | b)
             if (a | b)
Line 622: Line 622:
             end
             end


have different semantics in Matlab. This is really a Matlab bug, but there is too much code out there that relies on this behaviour to change it. Prefer the <tt>||</tt> and <tt>&&</tt> operators in <tt>if</tt> statements if possible. If you need to use code written for Matlab that depends on this buggy behaviour, you can enable it since Octave 3.4.0 with the following command:
have different semantics in Matlab. This is really a Matlab bug, but there is too much code out there that relies on this behaviour to change it. Prefer the {{Codeline|&#124;&#124;}} and {{Codeline|&&}} operators in {{Codeline|if}} statements if possible. If you need to use code written for Matlab that depends on this buggy behaviour, you can enable it since Octave 3.4.0 with the following command:


             do_braindead_shortcircuit_evaluation(1)
             do_braindead_shortcircuit_evaluation(1)
Line 640: Line 640:
             if ([1, 1] | [1, 2, 3]) 1, end  ## OK
             if ([1, 1] | [1, 2, 3]) 1, end  ## OK


Also Matlab requires the operands of <tt>&&</tt> and <tt>||</tt> to be scalar values but Octave does not (it just applies the rule that for an operand to be considered true, every element of the object must be nonzero or logically true).
Also Matlab requires the operands of {{Codeline|&&}} and {{Codeline|&#124;&#124;}} to be scalar values but Octave does not (it just applies the rule that for an operand to be considered true, every element of the object must be nonzero or logically true).


Finally, note the inconsistence of thinking of the condition of an <tt>if</tt> statement as being equivalent to <tt>all(X(:))</tt> when <tt>X</tt> is a matrix. This is true for all cases EXCEPT empty matrices:
Finally, note the inconsistence of thinking of the condition of an {{Codeline|if}} statement as being equivalent to {{Codeline|all(X(:))}} when {{Codeline|X}} is a matrix. This is true for all cases EXCEPT empty matrices:


             if ([0, 1]) == if (all ([0, 1]))  ==>  i.e., condition is false.
             if ([0, 1]) == if (all ([0, 1]))  ==>  i.e., condition is false.
Line 655: Line 655:
             if (all ([]))
             if (all ([]))


because, despite the name, the <tt>all</tt> is really returning true if none of the elements of the matrix are zero, and since there are no elements, well, none of them are zero. This is an example of [http://en.wikipedia.org/wiki/Vacuous_truth vacuous truth]. But, somewhere along the line, someone decided that <tt>if ([])</tt> should be false. Mathworks probably thought it just looks wrong to have <tt>[]</tt> be true in this context even if you can use logical gymnastics to convince yourself that "all" the elements of an empty matrix are nonzero. Octave however duplicates this behavior for <tt>if</tt> statements containing empty matrices.
because, despite the name, the {{Codeline|all}} is really returning true if none of the elements of the matrix are zero, and since there are no elements, well, none of them are zero. This is an example of [http://en.wikipedia.org/wiki/Vacuous_truth vacuous truth]. But, somewhere along the line, someone decided that {{Codeline|if ([])}} should be false. Mathworks probably thought it just looks wrong to have {{Codeline|[]}} be true in this context even if you can use logical gymnastics to convince yourself that "all" the elements of an empty matrix are nonzero. Octave however duplicates this behavior for {{Codeline|if}} statements containing empty matrices.


===Solvers for singular, under- and over-determined matrices===
===Solvers for singular, under- and over-determined matrices===
Line 700: Line 700:
The extensions in Octave over Matlab syntax are very useful, but might cause issues when sharing with Matlab users. A list of the major extensions that should be avoided to be compatible with Matlab are:
The extensions in Octave over Matlab syntax are very useful, but might cause issues when sharing with Matlab users. A list of the major extensions that should be avoided to be compatible with Matlab are:


Comments in octave can be marked with <tt>#</tt>. This allows POSIX systems to have the first line as <tt>#! octave -q</tt> and mark the script itself executable. Matlab doesn't have this feature due to the absence of comments starting with <tt>#</tt>".
Comments in octave can be marked with {{Codeline|#}}. This allows POSIX systems to have the first line as {{Codeline|#! octave -q}} and mark the script itself executable. Matlab doesn't have this feature due to the absence of comments starting with {{Codeline|#}}".


Code blocks like if, for, while, etc can be terminated with block specific terminations like endif. Matlab doesn't have this and all blocks must be terminated with end.
Code blocks like if, for, while, etc can be terminated with block specific terminations like endif. Matlab doesn't have this and all blocks must be terminated with end.


Octave has a lisp-like <tt>unwind_protect</tt> block that allows blocks of code that terminate in an error to ensure that the variables that are touched are restored. You can do something similar with try/catch combined with <tt>rethrow (lasterror ())</tt> in Matlab, however rethrow and lasterror are only available in Octave 2.9.10 and later. Matlab 2008a also introduced <tt>OnCleanUp</tt> that is similar to <tt>unwind_protect</tt>, except that the object created by this function has to be explicitly cleared in order for the cleanup code to run.
Octave has a lisp-like {{Codeline|unwind_protect}} block that allows blocks of code that terminate in an error to ensure that the variables that are touched are restored. You can do something similar with try/catch combined with {{Codeline|rethrow (lasterror ())}} in Matlab, however rethrow and lasterror are only available in Octave 2.9.10 and later. Matlab 2008a also introduced {{Codeline|OnCleanUp}} that is similar to {{Codeline|unwind_protect}}, except that the object created by this function has to be explicitly cleared in order for the cleanup code to run.


Note that using try/catch combined with <tt>rethrow (lasterror ())</tt> can not guarantee that global variables will be correctly reset, as it won't catch user interrupts with Ctrl-C. For example
Note that using try/catch combined with {{Codeline|rethrow (lasterror ())}} can not guarantee that global variables will be correctly reset, as it won't catch user interrupts with Ctrl-C. For example


                 global a
                 global a
Line 737: Line 737:
Typing Ctrl-C in the first case returns the user directly to the prompt, and the variable ''a'' is not reset to the saved value. In the second case the variable ''a'' is reset correctly. Therefore Matlab gives no safe way of temporarily changing global variables.
Typing Ctrl-C in the first case returns the user directly to the prompt, and the variable ''a'' is not reset to the saved value. In the second case the variable ''a'' is reset correctly. Therefore Matlab gives no safe way of temporarily changing global variables.


Indexing can be applied to all objects in Octave and not just variables. Therefore <tt>sin(x)(1:10);</tt> for example is perfectly valid in Octave but not Matlab. To do the same in Matlab you must do <tt>y = sin(x); y = y([1:10]);</tt>
Indexing can be applied to all objects in Octave and not just variables. Therefore {{Codeline|sin(x)(1:10)}} for example is perfectly valid in Octave but not Matlab. To do the same in Matlab you must do {{Codeline|y &#61; sin(x); y &#61; y([1:10]);}}


Octave has the operators <tt>++</tt>, <tt></tt>, <tt>-=</tt>, <tt>+=</tt>, <tt>*=</tt>, etc. As Matlab doesn't, if you are sharing code these should be avoided.
Octave has the operators {{Codeline|++}}, {{Codeline|}}, {{Codeline|-&#61;}}, {{Codeline|+&#61;}}, {{Codeline|*&#61;}}, etc. As Matlab doesn't, if you are sharing code these should be avoided.


Character strings in Octave can be denoted with double or single quotes. There is a subtle difference between the two in that escaped characters like <tt>\n</tt> (newline), <tt>\t</tt> (tab), etc are interpreted in double quoted strings but not single quoted strings. This difference is important on Windows platforms where the <tt>\</tt> character is used in path names, and so single quoted strings should be used in paths. Matlab doesn't have double quoted strings and so they should be avoided if the code will be transferred to a Matlab user.
Character strings in Octave can be denoted with double or single quotes. There is a subtle difference between the two in that escaped characters like {{Codeline|\n}} (newline), {{Codeline|\t}} (tab), etc are interpreted in double quoted strings but not single quoted strings. This difference is important on Windows platforms where the {{Codeline|\}} character is used in path names, and so single quoted strings should be used in paths. Matlab doesn't have double quoted strings and so they should be avoided if the code will be transferred to a Matlab user.


==GUI==
==GUI==