https://wiki.octave.org/wiki/index.php?title=Differences_between_Octave_and_Matlab&feed=atom&action=historyDifferences between Octave and Matlab - Revision history2021-05-08T10:59:47ZRevision history for this page on the wikiMediaWiki 1.35.2https://wiki.octave.org/wiki/index.php?title=Differences_between_Octave_and_Matlab&diff=13736&oldid=prevSiko1056: Add some nice blog entry.2021-04-21T01:19:25Z<p>Add some nice blog entry.</p>
<table class="diff diff-contentalign-left diff-editfont-monospace" data-mw="interface">
<col class="diff-marker" />
<col class="diff-content" />
<col class="diff-marker" />
<col class="diff-content" />
<tr class="diff-title" lang="en">
<td colspan="2" style="background-color: #fff; color: #202122; text-align: center;">← Older revision</td>
<td colspan="2" style="background-color: #fff; color: #202122; text-align: center;">Revision as of 01:19, 21 April 2021</td>
</tr><tr><td colspan="2" class="diff-lineno" id="mw-diff-left-l435" >Line 435:</td>
<td colspan="2" class="diff-lineno">Line 435:</td></tr>
<tr><td class='diff-marker'> </td><td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"></td><td class='diff-marker'> </td><td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"></td></tr>
<tr><td class='diff-marker'> </td><td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div>Function assert have extended input possibilities.</div></td><td class='diff-marker'> </td><td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div>Function assert have extended input possibilities.</div></td></tr>
<tr><td colspan="2"> </td><td class='diff-marker'>+</td><td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;"><div><ins style="font-weight: bold; text-decoration: none;"></ins></div></td></tr>
<tr><td colspan="2"> </td><td class='diff-marker'>+</td><td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;"><div><ins style="font-weight: bold; text-decoration: none;">== External links ==</ins></div></td></tr>
<tr><td colspan="2"> </td><td class='diff-marker'>+</td><td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;"><div><ins style="font-weight: bold; text-decoration: none;"></ins></div></td></tr>
<tr><td colspan="2"> </td><td class='diff-marker'>+</td><td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;"><div><ins style="font-weight: bold; text-decoration: none;">* https://statanalytica.com/blog/matlab-vs-octave/</ins></div></td></tr>
<tr><td class='diff-marker'> </td><td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"></td><td class='diff-marker'> </td><td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"></td></tr>
<tr><td class='diff-marker'> </td><td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div>[[Category:FAQ]]</div></td><td class='diff-marker'> </td><td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div>[[Category:FAQ]]</div></td></tr>
</table>Siko1056https://wiki.octave.org/wiki/index.php?title=Differences_between_Octave_and_Matlab&diff=13697&oldid=prev67.204.154.47: /* Unwind-protect */2021-03-17T19:17:05Z<p><span dir="auto"><span class="autocomment">Unwind-protect</span></span></p>
<table class="diff diff-contentalign-left diff-editfont-monospace" data-mw="interface">
<col class="diff-marker" />
<col class="diff-content" />
<col class="diff-marker" />
<col class="diff-content" />
<tr class="diff-title" lang="en">
<td colspan="2" style="background-color: #fff; color: #202122; text-align: center;">← Older revision</td>
<td colspan="2" style="background-color: #fff; color: #202122; text-align: center;">Revision as of 19:17, 17 March 2021</td>
</tr><tr><td colspan="2" class="diff-lineno" id="mw-diff-left-l368" >Line 368:</td>
<td colspan="2" class="diff-lineno">Line 368:</td></tr>
<tr><td class='diff-marker'> </td><td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div>The unwind_protect statement is often used to reliably restore the values of global variables that need to be temporarily changed.</div></td><td class='diff-marker'> </td><td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div>The unwind_protect statement is often used to reliably restore the values of global variables that need to be temporarily changed.</div></td></tr>
<tr><td class='diff-marker'> </td><td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"></td><td class='diff-marker'> </td><td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"></td></tr>
<tr><td class='diff-marker'>−</td><td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;"><div>Matlab can be made to do something similar with their {{<del class="diffchange diffchange-inline">manaul</del>|onCleanup}} function that was introduced in 2008a. Octave also has {{manual|onCleanup}} since version 3.4.0.</div></td><td class='diff-marker'>+</td><td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;"><div>Matlab can be made to do something similar with their {{<ins class="diffchange diffchange-inline">manual</ins>|onCleanup}} function that was introduced in 2008a. Octave also has {{manual|onCleanup}} since version 3.4.0.</div></td></tr>
<tr><td class='diff-marker'> </td><td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"></td><td class='diff-marker'> </td><td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"></td></tr>
<tr><td class='diff-marker'> </td><td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div>===Built-in ODE and DAE solvers===</div></td><td class='diff-marker'> </td><td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div>===Built-in ODE and DAE solvers===</div></td></tr>
</table>67.204.154.47https://wiki.octave.org/wiki/index.php?title=Differences_between_Octave_and_Matlab&diff=13175&oldid=prevSiko1056: Add Category:FAQ.2020-06-11T05:46:21Z<p>Add <a href="/Category:FAQ" title="Category:FAQ">Category:FAQ</a>.</p>
<table class="diff diff-contentalign-left diff-editfont-monospace" data-mw="interface">
<col class="diff-marker" />
<col class="diff-content" />
<col class="diff-marker" />
<col class="diff-content" />
<tr class="diff-title" lang="en">
<td colspan="2" style="background-color: #fff; color: #202122; text-align: center;">← Older revision</td>
<td colspan="2" style="background-color: #fff; color: #202122; text-align: center;">Revision as of 05:46, 11 June 2020</td>
</tr><tr><td colspan="2" class="diff-lineno" id="mw-diff-left-l435" >Line 435:</td>
<td colspan="2" class="diff-lineno">Line 435:</td></tr>
<tr><td class='diff-marker'> </td><td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"></td><td class='diff-marker'> </td><td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"></td></tr>
<tr><td class='diff-marker'> </td><td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div>Function assert have extended input possibilities.</div></td><td class='diff-marker'> </td><td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div>Function assert have extended input possibilities.</div></td></tr>
<tr><td colspan="2"> </td><td class='diff-marker'>+</td><td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;"><div><ins style="font-weight: bold; text-decoration: none;"></ins></div></td></tr>
<tr><td colspan="2"> </td><td class='diff-marker'>+</td><td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;"><div><ins style="font-weight: bold; text-decoration: none;">[[Category:FAQ]]</ins></div></td></tr>
</table>Siko1056https://wiki.octave.org/wiki/index.php?title=Differences_between_Octave_and_Matlab&diff=12964&oldid=prevSiko1056: Extract FAQ section to own page.2020-05-12T05:07:50Z<p>Extract <a href="/FAQ" title="FAQ">FAQ</a> section to own page.</p>
<p><b>New page</b></p><div>People often ask<br />
<br />
<blockquote><br />
I wrote some code for Matlab, and I want to get it running under Octave. Is there anything I should watch out for?<br />
</blockquote><br />
<br />
or alternatively<br />
<br />
<blockquote><br />
I wrote some code in Octave, and want to share it with Matlab users. Is there anything I should watch out for?<br />
</blockquote><br />
<br />
which is not quite the same thing. There are still a number of differences between Octave and Matlab, however in general differences between the two are considered as bugs. Octave might consider that the bug is in Matlab and do nothing about it, but generally functionality is almost identical. If you find an important functional difference between Octave behavior and Matlab, then you should send a description of this difference (with code illustrating the difference, if possible) to http://bugs.octave.org.<br />
<br />
Furthermore, Octave adds a few syntactical extensions to Matlab that might cause some issues when exchanging files between Matlab and Octave users.<br />
<br />
As both Octave and Matlab are under constant development, the information in this section is subject to change.<br />
<br />
You should also look at the pages http://octave.sourceforge.net/packages.php and http://octave.sourceforge.net/docs.html that have a function reference that is up to date. You can use this function reference to see the number of octave functions that are available and their Matlab compatibility.<br />
<br />
==Graphical Curve Fitting Tool==<br />
<br />
Currently, Octave lacks a graphical curve fitting tool such as Matlab's [https://www.mathworks.com/help/curvefit/curvefitting-app.html curvefit] tool. [https://lists.gnu.org/archive/html/help-octave/2015-07/msg00300.html]<br />
<br />
You can use polyfit or the packages [https://octave.sourceforge.io/signal/ signal] or [https://octave.sourceforge.io/optim/ optim] to achieve these task but these are not graphical tools.<br />
<br />
==Nested Functions==<br />
<br />
Octave has limited support for nested functions since version 3.8.0. That is<br />
<br />
<syntaxhighlight lang="Matlab"><br />
function y = foo (x)<br />
y = bar(x)<br />
function y = bar (x)<br />
y = ...;<br />
end<br />
end<br />
</syntaxhighlight><br />
<br />
is equivalent to<br />
<br />
<syntaxhighlight lang="Octave"><br />
function y = foo (x)<br />
y = bar(x)<br />
endfunction<br />
<br />
function y = bar (x)<br />
y = ...;<br />
endfunction<br />
</syntaxhighlight><br />
<br />
The main difference with Matlab is a matter of scope. While nested functions have access to the parent function's scope in Matlab, no such thing is available in Octave, due to how Octave essentially “un-nests” nested functions.<br />
<br />
The authors of Octave consider the nested function scoping rules of Matlab to be more problems than they are worth as they introduce difficult to find bugs as inadvertently modifying a variable in a nested function that is also used in the parent is particularly easy for those not attentive to detail.<br />
<br />
==Differences in core syntax==<br />
<br />
There are a few core Matlab syntaxes that are not accepted by Octave, these being<br />
<br />
* Some limitations on the use of function handles. The major difference is related to nested function scoping rules (as above) and their use with function handles.<br />
<br />
* Some limitations of variable argument lists on the LHS of an expression, though the most common types are accepted.<br />
<br />
* Matlab classdef object oriented programming is only partially supported, see [[classdef]] for details.<br />
<br />
==Differences in core functions==<br />
<br />
A large number of the Matlab core functions (i.e. those that are in the core and not a toolbox) are implemented, and certainly all of the commonly used ones. There are a few functions that aren't implemented, usually to do with specific missing Octave functionality (GUI, DLL, Java, ActiveX, DDE, web, and serial functions). Some of the core functions have limitations that aren't in the Matlab version. For example the {{manual|sprandn}} function can not force a particular condition number for the matrix like Matlab can. Another example is that testing and the runtests function work differently in Matlab and Octave.<br />
<br />
==Just-In-Time compiler==<br />
<br />
Matlab includes a "Just-In-Time" compiler. This compiler allows the acceleration of for-loops in Matlab to almost native performance with certain restrictions. The JIT must know the return type of all functions called in the loops and so you can't include user functions in the loop of JIT optimized loops. Octave has a [[JIT|not fully functional JIT compiler]]. For this reason you must [[Performance#Vectorization|vectorize your code]] as much as possible. The MathWorks themselves have a good document discussing vectorization at http://www.mathworks.com/support/tech-notes/1100/1109.html.<br />
<br />
==Compiler==<br />
<br />
On a related point, there is no Octave compiler, and so you can't convert your Octave code into a binary for additional speed or distribution.<br />
<br />
==Graphic handles==<br />
<br />
The support for graphics handles is converging towards full compatibility. If you notice any incompatibilities, please [http://bugs.octave.org report a bug].<br />
<br />
==GUI functions==<br />
<br />
The support for [http://www.octave.org/doc/interpreter/GUI-Development.html Matlab compatible GUI functions] was added in Octave version 3.6.0 and is converging towards full compatibility. If you notice any incompatibilities, please [http://bugs.octave.org report a bug].<br />
<br />
==Simulink==<br />
<br />
Octave itself includes no Simulink support. Typically the Simulink models lag research and are less flexible, so shouldn't really be used in a research environment. However, some Matlab users that try to use Octave complain about this lack.<br />
<br />
==MEX-Files==<br />
<br />
Octave includes an [http://www.octave.org/doc/interpreter/Mex_002dFiles.html API to the Matlab MEX interface]. However, as MEX is an API to the internals of Matlab and the internals of Octave differ from Matlab, there is necessarily a manipulation of the data to convert from a MEX interface to the Octave equivalent. This is notable for all complex matrices, where Matlab stores complex arrays as real and imaginary parts, whereas Octave respects the C99/C++ standards of co-locating the real/imag parts in memory. Also due to the way Matlab allows access to the arrays passed through a pointer, the MEX interface might require copies of arrays (even non complex ones).<br />
<br />
==Block comments==<br />
<br />
Block comments denoted by <code>#{</code> and <code>#}</code> markers (or <code>%{</code> and <code>%}</code>) are supported by Octave with some limitations. The major limitation is that block comments are not supported within [] or {}.<br />
<br />
==Mat-File format==<br />
<br />
There are some differences in the mat v5 file format accepted by Octave. Matlab recently introduced the "-V7.3" save option which is an HDF5 format which is particularly useful for 64-bit platforms where the standard Matlab format can not correctly save variables. Octave accepts HDF5 files, but is not yet compatible with the "-v7.3" versions produced by Matlab.<br />
<br />
Although Octave can load inline function handles saved by Matlab, it can not yet save them.<br />
<br />
Finally, some multi-byte Unicode characters aren't yet treated in mat-files.<br />
<br />
==Profiler==<br />
<br />
Thanks to Daniel Kraft's 2011 Google Summer of Code project, [http://www.octave.org/doc/interpreter/Profiling.html Octave has a profiler] since version 3.6.0.<br />
<br />
==Toolboxes==<br />
<br />
Octave is a community project and so the toolboxes that exist are donated by those interested in them through [[Octave Forge]]. These might be lacking in certain functionality relative to the Matlab toolboxes, and might not exactly duplicate the Matlab functionality or interface.<br />
<br />
==Short-circuit <code>&</code> and <code>|</code> operators==<br />
<br />
The <code>&</code> and <code>|</code> 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 <code>&&</code> and <code>||</code> short circuit. Note that this means that<br />
<br />
<syntaxhighlight lang="Octave"><br />
if (a | b)<br />
...<br />
end<br />
</syntaxhighlight><br />
<br />
and<br />
<br />
<syntaxhighlight lang="Octave"><br />
t = a | b;<br />
if (t)<br />
...<br />
end<br />
</syntaxhighlight><br />
<br />
have different semantics in Matlab. This is really a Matlab bug, but there is too much code out there that relies on this behavior to change it. Prefer the <code>&&</code> and <code>||</code> operators in {{Codeline|if}} statements if possible.<br />
<br />
Note that the difference with Matlab is also significant when either argument is a function with side effects or if the first argument is a scalar and the second argument is an empty matrix. For example, note the difference between<br />
<br />
<syntaxhighlight lang="Octave"><br />
t = 1 | []; ## results in [], so...<br />
if (t) 1, end ## in if ([]), this is false.<br />
</syntaxhighlight><br />
<br />
and<br />
<br />
<syntaxhighlight lang="Octave"><br />
if (1 | []) 1, end ## short circuits so condition is true.<br />
</syntaxhighlight><br />
<br />
In the latter case, Octave displays since version 4.0.0 a warning:<br />
<br />
warning: Matlab-style short-circuit operation performed for operator |<br />
<br />
Another case that is documented in the Matlab manuals is that<br />
<br />
<syntaxhighlight lang="Matlab"><br />
t = [1, 1] | [1, 2, 3]; ## error<br />
if ([1, 1] | [1, 2, 3]) 1, end ## OK<br />
</syntaxhighlight><br />
<br />
Also Matlab requires the operands of <code>&&</code> and <code>||</code> 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).<br />
<br />
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:<br />
<br />
<syntaxhighlight lang="Matlab"><br />
if ([0, 1]) == if (all ([0, 1])) ==> i.e., condition is false.<br />
if ([1, 1]) == if (all ([1, 1])) ==> i.e., condition is true.<br />
</syntaxhighlight><br />
<br />
However,<br />
<br />
<syntaxhighlight lang="Matlab"><br />
if ([])<br />
</syntaxhighlight><br />
<br />
is not the same as<br />
<br />
<syntaxhighlight lang="Matlab"><br />
if (all ([]))<br />
</syntaxhighlight><br />
<br />
because, despite the name, the {{manual|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 [https://en.wikipedia.org/wiki/Vacuous_truth vacuous truth]. But, somewhere along the line, someone decided that {{Codeline|if ([])}} should be false. The 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.<br />
<br />
==Solvers for singular, under- and over-determined matrices==<br />
<br />
Matlab's solvers as used by the operators {{manual|mldivide}} <code>\</code> and {{manual|mrdivide}} <code>/</code>, use a different approach than Octave's in the case of singular, under-, or over-determined matrices. In the case of a singular matrix, Matlab returns the result given by the LU decomposition, even though the underlying solver has flagged the result as erroneous. Octave has made the choice of falling back to a minimum norm solution of matrices that have been flagged as singular which arguably is a better result for these cases.<br />
<br />
In the case of under- or over-determined matrices, Octave continues to use a minimum norm solution, whereas Matlab uses an approach that is equivalent to<br />
<br />
<syntaxhighlight lang="Octave"><br />
function x = mldivide (A, b)<br />
m = rows (A);<br />
[Q, R, E] = qr (A);<br />
x = [A \ b, E(:, 1:m) * (R(:, 1:m) \ (Q' * b))]<br />
end<br />
</syntaxhighlight><br />
<br />
While this approach is certainly faster and uses less memory than Octave's minimum norm approach, this approach seems to be inferior in other ways.<br />
<br />
A numerical question arises: how big can the null space component become, relative to the minimum-norm solution? Can it be nicely bounded, or can it be arbitrarily big? Consider this example:<br />
<br />
<syntaxhighlight lang="Octave"><br />
m = 10;<br />
n = 10000;<br />
A = ones (m, n) + 1e-6 * randn (m, n);<br />
b = ones (m, 1) + 1e-6 * randn (m, 1);<br />
norm (A \ b)<br />
</syntaxhighlight><br />
<br />
while Octave's minimum-norm values are about 3e-2, Matlab's results are 50-times larger. For another issue, try this code:<br />
<br />
<syntaxhighlight lang="Octave"><br />
m = 5;<br />
n = 100;<br />
j = floor (m * rand (1, n)) + 1;<br />
b = ones (m, 1);<br />
A = zeros (m, n);<br />
A(sub2ind(size(A),j,1:n)) = 1;<br />
x = A \ b;<br />
[~,p] = sort (rand (1, n));<br />
y = A(:,p) \ b;<br />
norm (x(p) - y)<br />
</syntaxhighlight><br />
<br />
It shows that unlike in Octave, {{manual|mldivide}} in Matlab is not invariant with respect to column permutations. If there are multiple columns of the same norm, permuting columns of the matrix gets you different result than permuting the solution vector. This will surprise many users.<br />
<br />
Since the {{manual|mldivide}} <code>\</code> and {{manual|mrdivide}} <code>/</code> operators are often part of a more complex expression, where there is no room to react to warnings or flags, it should prefer intelligence (robustness) to speed, and so the Octave developers are firmly of the opinion that Octave's approach for singular, under- and over-determined matrices is a better choice than Matlab's.<br />
<br />
==Octave extensions==<br />
<br />
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:<br />
<br />
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|#}}".<br />
<br />
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.<br />
<br />
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.<br />
<br />
Note that using try/catch combined with {{Codeline|rethrow (lasterror ())}} cannot guarantee that global variables will be correctly reset, as it won't catch user interrupts with Ctrl-C. For example<br />
<br />
<syntaxhighlight lang="Matlab"><br />
global a<br />
a = 1;<br />
try<br />
_a = a;<br />
a = 2<br />
while true<br />
end<br />
catch<br />
fprintf ('caught interrupt\n');<br />
a = _a;<br />
rethrow (lasterror());<br />
end<br />
</syntaxhighlight><br />
<br />
compared to<br />
<br />
<syntaxhighlight lang="Octave"><br />
global a<br />
a = 1;<br />
unwind_protect<br />
_a = a;<br />
a = 2<br />
while true<br />
end<br />
unwind_protect_cleanup<br />
fprintf ('caught interrupt\n');<br />
a = _a;<br />
end<br />
</syntaxhighlight><br />
<br />
Typing Ctrl-C in the first case returns the user directly to the prompt, and the variable <code>a</code> is not reset to the saved value. In the second case the variable <code>a</code> is reset correctly. Therefore Matlab gives no safe way of temporarily changing global variables.<br />
<br />
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]);}}<br />
<br />
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.<br />
<br />
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.<br />
<br />
==What features are unique to Octave?==<br />
<br />
Although most of the Octave language will be familiar to Matlab users, it has some unique features of its own.<br />
<br />
=== Functions defined on the command-line===<br />
Functions can be defined by entering code on the command line, a feature not supported by Matlab. For example, you may type:<br />
<br />
<syntaxhighlight lang="Octave"><br />
>> function s = hello_string (to_who)<br />
> ## Say hello<br />
> if nargin<1, to_who = "World"; end<br />
> s = ["Hello ",\<br />
> to_who];<br />
> endfunction<br />
>> hello_string ("Moon")<br />
ans = Hello Moon<br />
</syntaxhighlight><br />
<br />
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 (...)}})<br />
<br />
Note: MATLAB R2016b added the ability to [http://www.mathworks.com/help/matlab/matlab_prog/local-functions-in-scripts.html define functions in script files].<br />
<br />
===Comments with #===<br />
<br />
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.<br />
<br />
===Strings delimited by double quotes "===<br />
<br />
In 2016, Matlab introduced String Arrays, that are initialized by using double quoted strings, and are not implemented in Octave yet. In Octave double-quoted strings include backslash interpretation (like C++, C, and Perl) while single quoted are uninterpreted (like Matlab and Perl).<br />
<br />
===Line continuation by backslash===<br />
<br />
Lines can be continued with a backslash, {{Codeline|\}}, in addition to three points {{Codeline|...}} as in Matlab.<br />
<br />
===Informative block closing===<br />
<br />
You may close function, for, while, if, ... blocks with endfunction, endfor, endwhile, ... keywords in addition to using end. As with Matlab, the end (or endfunction) keyword that marks the end of a function defined in a .m file is optional.<br />
<br />
===Coherent syntax===<br />
<br />
Indexing other things than variables is possible, as in:<br />
<br />
<syntaxhighlight lang="Octave"><br />
>> [3 1 4 1 5 9](3)<br />
ans = 4<br />
>> cos([0 pi pi/4 7])(3)<br />
ans = 0.70711<br />
</syntaxhighlight><br />
<br />
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.<br />
<br />
===Exclamation mark as not operator===<br />
<br />
The exclamation mark {{Codeline|!}} (aka “Bang!”) is a negation operator, just like the tilde {{Codeline|~}}:<br />
<br />
<syntaxhighlight lang="Octave"><br />
>> if ! strcmp (program_name, "octave"),<br />
> "It's an error"<br />
> else<br />
> "It works!"<br />
> end<br />
ans = It works!<br />
</syntaxhighlight><br />
<br />
Note however that Matlab uses the {{Codeline|!}} operator for shell escapes, for which Octave requires using the system command.<br />
<br />
===Increment and decrement operators===<br />
<br />
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|+&#61;}}, {{Codeline|-&#61;}}, {{Codeline|*&#61;}}, {{Codeline|/&#61;}}, {{Codeline|^&#61;}},{{Codeline|.+&#61;}},{{Codeline|.-&#61;}},{{Codeline|.*&#61;}}, {{Codeline|./&#61;}} and {{Codeline|.^&#61;}}.<br />
<br />
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}}.<br />
<br />
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.<br />
<br />
For matrix and vector arguments, the increment and decrement operators work on each element of the operand.<br />
<br />
===Unwind-protect===<br />
<br />
In addition to try-catch blocks, Octave supports an alternative form of exception handling modeled after the unwind-protect form of Lisp. The general form of an unwind_protect block looks like this:<br />
<br />
<syntaxhighlight lang="Octave"><br />
unwind_protect<br />
body<br />
unwind_protect_cleanup<br />
cleanup<br />
end_unwind_protect<br />
</syntaxhighlight><br />
<br />
Where body and cleanup are both optional and may contain any Octave expressions or commands. The statements in cleanup are guaranteed to be executed regardless of how control exits body.<br />
<br />
The unwind_protect statement is often used to reliably restore the values of global variables that need to be temporarily changed.<br />
<br />
Matlab can be made to do something similar with their {{manaul|onCleanup}} function that was introduced in 2008a. Octave also has {{manual|onCleanup}} since version 3.4.0.<br />
<br />
===Built-in ODE and DAE solvers===<br />
<br />
Octave includes LSODE, DASSL and DASPK for solving systems of stiff ordinary differential and differential-algebraic equations. These functions are built in to the interpreter.<br />
<br />
===Do-Until loop structure===<br />
<br />
Similar to the do-while loop in C and C++, Octave allows a do-until loop which does not exist in Matlab:<br />
<br />
<syntaxhighlight lang="Octave"><br />
x = 0<br />
do<br />
x += 1;<br />
until (x == 10)<br />
</syntaxhighlight><br />
<br />
===Broadcasting===<br />
<br />
Borrowed from [http://stackoverflow.com/q/26948776/3565696 other languages], [http://www.octave.org/doc/interpreter/Broadcasting.html octave broadcasting] allows easy and readable vectorization.<br />
<br />
<syntaxhighlight lang="Octave"><br />
f = (1:0.1:2);<br />
# put angular frequencies on the first dimension to prepare broadcasting<br />
omega = 2 * pi * f(:);<br />
# time is already on the second dimension (row vector)<br />
t = 0:0.02:2;<br />
# the resulting s will be a 2-dimensional array<br />
s = sin(omega .* t);<br />
# which can be displayed as<br />
pcolor(t, f, s)<br />
xlabel("t (s)")<br />
ylabel("f (Hz)")<br />
</syntaxhighlight><br />
<br />
Note: [https://www.mathworks.com/help/matlab/matlab_prog/compatible-array-sizes-for-basic-operations.html Automatic expansion of dimensions] was added to MATLAB R2016b.<br />
<br />
===Documentation strings===<br />
<br />
Octave allows extensive formatting of the help string of functions using Texinfo. The effect on the online documentation is relatively small, but makes the help string of functions conform to the help of Octave’s own functions. However, the effect on the appearance of printed or online documentation will be greatly improved.<br />
<br />
===Test functions===<br />
<br />
Octave allows to add self-tests to user defined functions. Tests are put after function definition in specially commented block.<br />
<br />
<syntaxhighlight lang="Octave"><br />
function mult = a(val)<br />
mult = val.*2;<br />
endfunction<br />
%!test<br />
%! assert (a(3), 6);<br />
</syntaxhighlight><br />
<br />
Such a function can be tested for valid outputs by following code:<br />
<br />
>> test a<br />
PASSES 1 out of 1 test<br />
<br />
===Demonstration Functions===<br />
<br />
Example code block can be part of function file in a similar manner as test functions. For example to run {{manual|demo}} for function multinom of package specfun, use:<br />
<br />
demo multinom<br />
<br />
===Powerful assert===<br />
<br />
Function assert have extended input possibilities.</div>Siko1056