Editing Tips and tricks
Jump to navigation
Jump to search
The edit can be undone. Please check the comparison below to verify that this is what you want to do, and then publish the changes below to finish undoing the edit.
Latest revision | Your text | ||
Line 1: | Line 1: | ||
=Tiny helper functions= | |||
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. | 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. | ||
== replace help with man == | |||
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). | |||
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 | |||
{{Code|alias to help|<pre> | {{Code|alias to help|<pre> | ||
Line 66: | Line 10: | ||
endfunction</pre>}} | endfunction</pre>}} | ||
=C++= | |||
== Real matrix operations== | |||
This is a table of matrix operations commonly performed in Octave and their equivalents in C++ when using the octave libraries. | This is a table of matrix operations commonly performed in Octave and their equivalents in C++ when using the octave libraries. | ||
Line 78: | Line 21: | ||
<tr><td>element multiplication</td><td><code>A.*B</code></td><td><code>product(A,B) </code></td></tr> | <tr><td>element multiplication</td><td><code>A.*B</code></td><td><code>product(A,B) </code></td></tr> | ||
<tr><td>element division</td><td><code>A./B</code></td><td><code>quotient(A,B) </code></td></tr> | <tr><td>element division</td><td><code>A./B</code></td><td><code>quotient(A,B) </code></td></tr> | ||
<tr><td>transpose*</td><td><code>A | <tr><td>transpose*</td><td><code>A'</code></td><td><code>A.transpose()</code></td></tr> | ||
<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> | <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> | ||
<tr><td>select row N of A**</td><td><code>A(N,:)</code></td><td><code>A.row(N-1)</code></td></tr> | <tr><td>select row N of A**</td><td><code>A(N,:)</code></td><td><code>A.row(N-1)</code></td></tr> | ||
Line 113: | Line 56: | ||
<tr><td>number of rows</td><td><code>size(A,1)</code></td><td><code>A.rows()</code></td></tr> | <tr><td>number of rows</td><td><code>size(A,1)</code></td><td><code>A.rows()</code></td></tr> | ||
<tr><td>number of columns</td><td><code>size(A,2)</code></td><td><code>A.cols()</code></td></tr> | <tr><td>number of columns</td><td><code>size(A,2)</code></td><td><code>A.cols()</code></td></tr> | ||
</table> | </table> | ||
Line 124: | Line 66: | ||
* 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. | * 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. | ||
==Complex Matrix Operations== | |||
<table> | <table> | ||
<tr><td><b>Operation</b></td><td><b>Octave</b></td><td><b>C++</b></td></tr> | <tr><td><b>Operation</b></td><td><b>Octave</b></td><td><b>C++</b></td></tr> | ||
Line 131: | Line 72: | ||
</table> | </table> | ||
===Vectorizing Tricks | =General= | ||
==A funny formatting trick with fprintf found by chance== | |||
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? | |||
That's easy: | |||
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 | |||
fprintf('%15.15s | %15.15s\n', Text{k,1}, Text{k,2}); | |||
The syntax '%<n>.<m>s' allocates '<n>' places to write chars and display the '<m>' first characters of the string to display. | |||
Example: | |||
octave:1> Text={'Hello','World'}; | |||
octave:2> fprintf('%15.15s | %15.15s\n', Text{1,1}, Text{1,2}) | |||
Hello | World | |||
==Load Comma Separated Values (*.csv) files== | |||
A=textread("file.csv", "%d", "delimiter", ","); | |||
B=textread("file.csv", "%s", "delimiter", ","); | |||
inds = isnan(A); | |||
B(!inds) = num2cell(A(!inds)) | |||
This gets you a 1 column cell array. You can reshape it to the original size by using the <code>reshape</code> function | |||
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 | |||
==Using Variable Strings in Octave Commands== | |||
For example, to plot data using a string variable as a legend: | |||
Option 1 (simplest): | |||
legend = "-1;My data;"; | |||
plot(x, y, legend); | |||
Option 2 (to insert variables): | |||
plot(x, y, sprintf("-1;%s;", dataName)); | |||
Option 3 (not as neat): | |||
legend = 'my legend'; | |||
plot_command = ['plot(x,y,\';',legend,';\')']; | |||
eval(plot_command); | |||
These same tricks are useful for reading and writing data files with unique names, etc. | |||
==Vectorizing Tricks== | |||
You can easily fill a vector with an index: | You can easily fill a vector with an index: | ||
for i=1:n, x(i) = i; end | for i=1:n, x(i) = i; end | ||
x = 1:n; | x = [1:n]; | ||
This works for expressions on the index by wrapping the index in an expression: | This works for expressions on the index by wrapping the index in an expression: | ||
for i=1:n, x(i) = sin(2*pi*i*f/r); end | for i=1:n, x(i) = sin(2*pi*i*f/r); end | ||
x = sin(2*pi* | x = sin(2*pi*[1:n]*f/r); | ||
You can also work with other vectors this way: | You can also work with other vectors this way: | ||
for i=1:n, x(i) = sin(2*pi*y(i)*f/r); end | for i=1:n, x(i) = sin(2*pi*y(i)*f/r); end | ||
Line 168: | Line 150: | ||
*tricks relying on fortran indexing | *tricks relying on fortran indexing | ||
===Other references=== | |||
*MATLAB array manipulation tips and tricks by Peter Acklam: http://home.online.no/~pjacklam/matlab/doc/mtt/index.html | |||
*MATLAB array manipulation tips and tricks by Peter Acklam: | |||
*The MathWorks: Code Vectorization Guide: http://www.mathworks.com/support/tech-notes/1100/1109.html | *The MathWorks: Code Vectorization Guide: http://www.mathworks.com/support/tech-notes/1100/1109.html | ||