https://wiki.octave.org/wiki/api.php?action=feedcontributions&user=129.187.68.65&feedformat=atomOctave - User contributions [en]2024-03-29T14:19:04ZUser contributionsMediaWiki 1.39.2https://wiki.octave.org/wiki/index.php?title=Tips_and_tricks&diff=1165Tips and tricks2012-05-24T07:35:26Z<p>129.187.68.65: /* Load Comma Separated Values (*.csv) files */ fixed a typo</p>
<hr />
<div>=Tiny helper functions=<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 />
== replace help with man ==<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 />
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 />
<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 />
<br />
=General=<br />
==A funny formatting trick with fprintf found by chance==<br />
<br />
Imagine that you want to create a text table with fprintf with 2 columns of 15 characters width and both right justified. How to do this thing?<br />
<br />
That's easy:<br />
<br />
If the variable Text is a cell array of strings (of length <15) with two columns and a certain number of rows, simply type for the kth row of Text<br />
fprintf('%15.15s | %15.15s\n', Text{k,1}, Text{k,2});<br />
The syntax '%<n>.<m>s' allocates '<n>' places to write chars and display the '<m>' first characters of the string to display.<br />
<br />
Example:<br />
octave:1> Text={'Hello','World'};<br />
octave:2> fprintf('%15.15s | %15.15s\n', Text{1,1}, Text{1,2})<br />
Hello | World<br />
<br />
==Load Comma Separated Values (*.csv) files==<br />
<br />
A=textread("file.csv", "%d", "delimiter", ",");<br />
B=textread("file.csv", "%s", "delimiter", ",");<br />
inds = isnan(A);<br />
B(!inds) = num2cell(A(!inds))<br />
<br />
This gets you a 1 column cell array. You can reshape it to the original size by using the <code>reshape</code> function<br />
<br />
The next version of octave (3.6) implements the <code>CollectOutput</code> switch as seen in example 8 here: http://www.mathworks.com/help/techdoc/ref/textscan.html<br />
<br />
==Using Variable Strings in Octave Commands==<br />
<br />
For example, to plot data using a string variable as a legend:<br />
<br />
Option 1 (simplest):<br />
legend = "-1;My data;";<br />
plot(x, y, legend);<br />
<br />
Option 2 (to insert variables):<br />
plot(x, y, sprintf("-1;%s;", dataName));<br />
<br />
Option 3 (not as neat):<br />
legend = 'my legend';<br />
plot_command = ['plot(x,y,\';',legend,';\')'];<br />
eval(plot_command);<br />
<br />
These same tricks are useful for reading and writing data files with unique names, etc.<br />
<br />
==Vectorizing Tricks==<br />
<br />
You can easily fill a vector with an index:<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 />
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 />
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 />
*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>129.187.68.65