https://wiki.octave.org/wiki/api.php?action=feedcontributions&user=88.76.47.115&feedformat=atomOctave - User contributions [en]2024-03-29T08:35:26ZUser contributionsMediaWiki 1.39.2https://wiki.octave.org/wiki/index.php?title=Tips_and_tricks&diff=2784Tips and tricks2013-05-30T17:19:10Z<p>88.76.47.115: /* Preferences */</p>
<hr />
<div>==Preferences==<br />
Sometimes, Octave defaults are not the best for someone's specific use. To change the defaults, use the following on the {{Path|.octaverc}} file.<br />
<br />
=== Changing default figure size ===<br />
The default size of a figure may be appropriate for simple figures but not so much when using {{Codeline|subplot}} for example. This can be changed though. <br />
<br />
{{Code|change default figure size|<pre>set (0, 'DefaultFigurePosition', [1 get(0, "screensize")(4:-1:3) get(0, "DefaultFigurePosition")(4)]);</pre>}}<br />
<br />
The value of {{Codeline|DefaultFigurePosition}} must be a four element vector with the x and y coordinates for the figure, followed by its with and height. The code above sets the default image to be placed at the top of the monitor, with the width of the monitor and the same height previously set as default.<br />
<br />
<br />
=== Shorten help message ===<br />
To get rid of the long help message with the link to the Octave homepage place this in your startup file usually at ~/.octaverc (if it does not exist, create it). See the [http://www.gnu.org/software/octave/doc/interpreter/Startup-Files.html documentation] for more information.<br />
suppress_verbose_help_message(1)<br />
<br />
==Tiny helper functions==<br />
<br />
This is a list of tiny helper functions (the equivalent of e.g., shell aliases), the kind one would have on its {{Path|.octaverc}} file.<br />
<br />
=== Reload 'octave.rc' after 'clear' ===<br />
When using {{Codeline|clear}}, one may accidentally remove functions (alias) or other variables set on the {{Path|octave.rc}} file. This can fixed by shadowing the {{Codeline|clear}} function with the following:<br />
<br />
{{Code|reload octave.rc after clear|<pre><br />
function clear (varargin)<br />
args = sprintf (', "%s"', varargin{:});<br />
evalin ("caller", ['builtin ("clear"' args ')']);<br />
source ("~/.octaverc");<br />
endfunction</pre>}}<br />
<br />
The problem with this approach is if there's path manipulation on the {{Path|octave.rc}} file, such as {{Codeline|addpath}}. A workaround is needed for each case since it is not possible to obtain a reliable list of what's in Octave load path. But basically should be to undo what the file does, before {{Codeline|source ("~/.octaverc")}}.<br />
<br />
If there's a {{Codeline|pkg unload all}} on it, this would also unload all packages. The following adjustment will keep the packages loaded<br />
<br />
{{Code|reload octave.rc after clear but keep packages loaded|<pre><br />
function clear (varargin)<br />
args = sprintf (', "%s"', varargin{:});<br />
evalin ("caller", ['builtin ("clear"' args ')']);<br />
pkglist = pkg ("list");<br />
loadedpkg = cell (0);<br />
for ii = 1:numel (pkglist)<br />
if (pkglist{ii}.loaded)<br />
loadedpkg{end+1} = pkglist{ii}.name;<br />
endif<br />
endfor<br />
source ("~/.octaverc");<br />
if (numel (loadedpkg) != 0)<br />
pkg ("load", loadedpkg{:});<br />
endif<br />
endfunction</pre>}}<br />
<br />
<br />
=== replace help with man ===<br />
<br />
If you use octave too much, you'll find yourself trying to use {{Codeline|help}} instead of {{Codeline|man}} on bash. This function will fix that so you can use {{Codeline|man}} in your octave instance (you can also do the opposite, create a {{Codeline|help}} alias in bash but {{Codeline|man}} has less characters).<br />
<br />
{{Code|alias to help|<pre><br />
function man (name)<br />
help (char (name))<br />
endfunction</pre>}}<br />
<br />
==C++==<br />
<br />
=== Real matrix operations===<br />
<br />
This is a table of matrix operations commonly performed in Octave and their equivalents in C++ when using the octave libraries.<br />
<br />
<table><br />
<tr><td><b>Operation</b></td><td><b>Octave</b></td><td><b>C++</b></td><tr><td>add</td><td><code>A+B</code></td><td><code>A+B</code></td></tr><br />
<tr><td>subtract</td><td><code>A-B</code></td><td><code>A-B</code></td></tr><br />
<tr><td>matrix multiplication</td><td><code>A*B</code></td><td><code>A*B</code></td></tr><br />
<tr><td>element multiplication</td><td><code>A.*B</code></td><td><code>product(A,B) </code></td></tr><br />
<tr><td>element division</td><td><code>A./B</code></td><td><code>quotient(A,B) </code></td></tr><br />
<tr><td>transpose*</td><td><code>A'</code></td><td><code>A.transpose()</code></td></tr><br />
<tr><td>select element m,n of A**</td><td><code>A(m,n)</code></td><td><code>A(m-1,n-1)</code></td></tr><br />
<tr><td>select row N of A**</td><td><code>A(N,:)</code></td><td><code>A.row(N-1)</code></td></tr><br />
<tr><td>select column N of A**</td><td><code>A(:,N)</code></td><td><code>A.column(N-1)</code></td></tr><br />
<tr><td>extract submatrix of A</td><td><code>A(a:b,c:d)</code></td><td><code>A.extract(a-1,c-1,b-1,d-1)</code></td></tr><br />
<tr><td>absolute value of A</td><td><code>abs(A)</code></td><td><code>A.abs()</code></td></tr><br />
<tr><td>comparison to scalar***</td><td><code>A&gt;2</code></td><td><code>mx_el_gt(A,2)</code></td></tr><br />
<tr><td></td><td><code>A&lt;2</code></td><td><code>mx_el_lt(A,2)</code></td></tr><br />
<tr><td></td><td><code>A==2</code></td><td><code>mx_el_eq(A,2)</code></td></tr><br />
<tr><td></td><td><code>A~=2</code></td><td><code>mx_el_ne(A,2)</code></td></tr><br />
<tr><td></td><td><code>A&gt;=2</code></td><td><code>mx_el_ge(A,2)</code></td></tr><br />
<tr><td></td><td><code>A&lt;=2</code></td><td><code>mx_el_le(A,2)</code></td></tr><br />
<tr><td>matrix of zeros</td><td><code>A=zeros(m,n)</code></td><td><code>A.fill(0.0)</code></td></tr><br />
<tr><td>matrix of ones</td><td><code>A=ones(m,n)</code></td><td><code>A.fill(1.0)</code></td></tr><br />
<tr><td>identity matrix</td><td><code>eye(N)</code></td><td><code>identity_matrix(N,N)</code></td></tr><br />
<tr><td>inverse of A</td><td><code>inv(A)</code></td><td><code>A.inverse()</code></td></tr><br />
<tr><td>pseudoinverse of A</td><td><code>pinv(A)</code></td><td><code>A.pseudo_inverse()</code></td></tr><br />
<tr><td>diagonal elements of A</td><td><code>diag(A)</code></td><td><code>A.diag()</code></td></tr><br />
<tr><td>column vector</td><td><code>A(:)</code></td><td><code>ColumnVector(A.reshape (dim_vector(A.length())))</code></td></tr><br />
<tr><td>row vector</td><td><code>A(:)'</code></td><td><code>RowVector(A.reshape (dim_vector(A.length())))</code></td></tr><br />
<tr><td>check for Inf or NaN</td><td><code>any(~isfinite(A))</code></td><td><code>A.any_element_is_inf_or_nan()</code></td></tr><br />
<tr><td>stack two matrices vertically</td><td><code>A=[B;C]</code></td><td><code>B.stack(C)</code></td></tr><br />
<tr><td>uniform random matrix</td><td><code>rand(a,b)</code></td><td><code>octave_rand::distribution("uniform"); octave_rand::matrix(a,b)</code></td></tr><br />
<tr><td>normal random matrix</td><td><code>randn(a,b)</code></td><td><code>octave_rand::distribution("normal"); octave_rand::matrix(a,b)</code></td></tr><br />
<tr><td>sum squares of columns</td><td><code>sumsq(A)</code></td><td><code>A.sumsq()</code></td></tr><br />
<tr><td>sum along columns</td><td><code>sum(A,1)</code></td><td><code>A.sum(0)</code></td></tr><br />
<tr><td>sum along rows</td><td><code>sum(A,2)</code></td><td><code>A.sum(1)</code></td></tr><br />
<tr><td>product along columns</td><td><code>prod(A,1)</code></td><td><code>A.prod(0)</code></td></tr><br />
<tr><td>product along rows</td><td><code>prod(A,2)</code></td><td><code>A.prod(1)</code></td></tr><br />
<tr><td>cumsum along columns</td><td><code>cumsum(A,1)</code></td><td><code>A.cumsum(0)</code></td></tr><br />
<tr><td>cumsum along rows</td><td><code>cumsum(A,2)</code></td><td><code>A.cumsum(1)</code></td></tr><br />
<tr><td>cumproduct along columns</td><td><code>cumprod(A,1)</code></td><td><code>A.cumprod(0)</code></td></tr><br />
<tr><td>cumproduct along rows</td><td><code>cumprod(A,2)</code></td><td><code>A.cumprod(1)</code></td></tr><br />
<tr><td>number of rows</td><td><code>size(A,1)</code></td><td><code>A.rows()</code></td></tr><br />
<tr><td>number of columns</td><td><code>size(A,2)</code></td><td><code>A.cols()</code></td></tr><br />
</table><br />
<br />
Notes:<br />
<br />
* Transpose, addition, and multiplication operations also apply to RowVector, ComplexRowVector, ColumnVector, and ComplexColumnVector data types when the dimensions are in agreement. <br />
<br />
* The difference is due to the fact that arrays are zero-based in C++, but one-based in Octave. <br />
<br />
* The names of Octave internal functions, such as mx_el_gt, are not documented and are subject to change. Functions such as mx_el_gt may eventually be available at both the scripting level and in C++ under more common names such as gt.<br />
<br />
===Complex Matrix Operations===<br />
<br />
<table><br />
<tr><td><b>Operation</b></td><td><b>Octave</b></td><td><b>C++</b></td></tr><br />
<tr><td>conjugate tranpose</td><td><code>A'</code></td><td><code>A.hermitian()</code></td></tr><br />
</table><br />
<br />
==General==<br />
<br />
===Vectorizing Tricks===<br />
<br />
You can easily fill a vector with an index:<br />
<br />
for i=1:n, x(i) = i; end<br />
<br />
x = 1:n;<br />
<br />
This works for expressions on the index by wrapping the index in an expression:<br />
<br />
for i=1:n, x(i) = sin(2*pi*i*f/r); end<br />
<br />
x = sin(2*pi*(1:n)*f/r);<br />
<br />
You can also work with other vectors this way:<br />
<br />
for i=1:n, x(i) = sin(2*pi*y(i)*f/r); end<br />
<br />
x = sin(2*pi*y*f/r);<br />
<br />
Conditionals in the for loop are a little bit tricky. We need to create an index vector for the true condition, and another for the false condition, then calculate the two independently.<br />
<br />
for i=1:n, if y(i)<1, x(i)=y(i); else x(i) = 2*y(i); endif<br />
<br />
idx = y < 1;<br />
x(idx) = y(idx);<br />
x(!idx) = 2*y(!idx);<br />
<br />
FIXME: add the following<br />
*examples from matrices<br />
*tricks with sort and cumsum (e.g., hist, lookup)<br />
*counter-examples such as a tridiagonal solver<br />
*sparse matrix tricks<br />
*tricks relying on fortran indexing<br />
<br />
====Other references====<br />
<br />
*MATLAB array manipulation tips and tricks by Peter Acklam: http://home.online.no/~pjacklam/matlab/doc/mtt/index.html<br />
*The MathWorks: Code Vectorization Guide: http://www.mathworks.com/support/tech-notes/1100/1109.html</div>88.76.47.115