Tips and tricks: Difference between revisions

no edit summary
(→‎Vectorizing Tricks: Use of brackets [] is unnecessary)
No edit summary
Line 1: Line 1:
=Tiny helper functions=
==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 ==
=== 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 less characters).


Line 10: Line 12:
  endfunction</pre>}}
  endfunction</pre>}}


=C++=
==C++==
 
=== Real matrix operations===


== 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 66: Line 69:
* 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==
===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 72: Line 76:
</table>
</table>


==General==


=General=
===A funny formatting trick with fprintf found by chance===
==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?
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?
Line 89: Line 93:
           Hello |          World
           Hello |          World


==Load Comma Separated Values (*.csv) files==
===Load Comma Separated Values (*.csv) files===


  A=textread("file.csv", "%d", "delimiter", ",");
  A=textread("file.csv", "%d", "delimiter", ",");
Line 100: Line 104:
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
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==
===Using Variable Strings in Octave Commands===


For example, to plot data using a string variable as a legend:
For example, to plot data using a string variable as a legend:
Line 118: Line 122:
These same tricks are useful for reading and writing data files with unique names, etc.
These same tricks are useful for reading and writing data files with unique names, etc.


==Vectorizing Tricks==
===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


Line 126: Line 131:


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


Line 131: Line 137:


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 150: Line 157:
*tricks relying on fortran indexing
*tricks relying on fortran indexing


===Other references===
====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: http://home.online.no/~pjacklam/matlab/doc/mtt/index.html
*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
364

edits