Difference between revisions of "Tips and tricks"
(→Using Variable Strings in Octave Commands)
(preferences -- change default figure size)
|Line 1:||Line 1:|
==Tiny helper functions==
==Tiny helper functions==
Revision as of 19:36, 24 January 2013
Sometimes, Octave defaults are not teh best for someone's specific use. To change the defaults, use the following on the .octaverc file.
Changing default figure size
The default size of a figure may be appropriate for simple figures but not so much when using
subplot for example. This can be changed though.
|Code: change default figure size|
set (0, 'DefaultFigurePosition', [1 get(0, "screensize")(4:-1:3) get(0, "DefaultFigurePosition")(4)]);
The value of
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.
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 .octaverc file.
replace help with man
If you use octave too much, you'll find yourself trying to use
help instead of
man on bash. This function will fix that so you can use
man in your octave instance (you can also do the opposite, create a
help alias in bash but
man has less characters).
|Code: alias to help|
function man (name) help (char (name)) endfunction
Real matrix operations
This is a table of matrix operations commonly performed in Octave and their equivalents in C++ when using the octave libraries.
|select element m,n of A**|
|select row N of A**|
|select column N of A**|
|extract submatrix of A|
|absolute value of A|
|comparison to scalar***|
|matrix of zeros|
|matrix of ones|
|inverse of A|
|pseudoinverse of A|
|diagonal elements of A|
|check for Inf or NaN|
|stack two matrices vertically|
|uniform random matrix|
|normal random matrix|
|sum squares of columns|
|sum along columns|
|sum along rows|
|product along columns|
|product along rows|
|cumsum along columns|
|cumsum along rows|
|cumproduct along columns|
|cumproduct along rows|
|number of rows|
|number of columns|
- Transpose, addition, and multiplication operations also apply to RowVector, ComplexRowVector, ColumnVector, and ComplexColumnVector data types when the dimensions are in agreement.
- The difference is due to the fact that arrays are zero-based in C++, but one-based in Octave.
- 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
You can easily fill a vector with an index:
for i=1:n, x(i) = i; end
x = 1:n;
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
x = sin(2*pi*(1:n)*f/r);
You can also work with other vectors this way:
for i=1:n, x(i) = sin(2*pi*y(i)*f/r); end
x = sin(2*pi*y*f/r);
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.
for i=1:n, if y(i)<1, x(i)=y(i); else x(i) = 2*y(i); endif
idx = y < 1; x(idx) = y(idx); x(!idx) = 2*y(!idx);
FIXME: add the following
- examples from matrices
- tricks with sort and cumsum (e.g., hist, lookup)
- counter-examples such as a tridiagonal solver
- sparse matrix tricks
- tricks relying on fortran indexing
- 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