Tips and tricks

From Octave
Revision as of 16:26, 26 November 2011 by Joannac (talk | contribs) (Ported C++ stuff)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search


Real matrix operations

This is a table of matrix operations commonly performed in Octave and their equivalents in C++ when using the octave libraries.

matrix multiplicationA*BA*B
element multiplicationA.*Bproduct(A,B)
element divisionA./Bquotient(A,B)
select element m,n of A**A(m,n)A(m-1,n-1)
select row N of A**A(N,:)A.row(N-1)
select column N of A**A(:,N)A.column(N-1)
extract submatrix of AA(a:b,c:d)A.extract(a-1,c-1,b-1,d-1)
absolute value of Aabs(A)A.abs()
comparison to scalar***A>2mx_el_gt(A,2)
matrix of zerosA=zeros(m,n)A.fill(0.0)
matrix of onesA=ones(m,n)A.fill(1.0)
identity matrixeye(N)identity_matrix(N,N)
inverse of Ainv(A)A.inverse()
pseudoinverse of Apinv(A)A.pseudo_inverse()
diagonal elements of Adiag(A)A.diag()
column vectorA(:)ColumnVector(A.reshape (dim_vector(A.length())))
row vectorA(:)'RowVector(A.reshape (dim_vector(A.length())))
check for Inf or <a href="">NaN</a>any(~isfinite(A))A.any_element_is_inf_or_nan()
stack two matrices verticallyA=[B;C]B.stack(C)
uniform random matrixrand(a,b)octave_rand::distribution("uniform"); octave_rand::matrix(a,b)
normal random matrixrandn(a,b)octave_rand::distribution("normal"); octave_rand::matrix(a,b)
sum squares of columnssumsq(A)A.sumsq()
sum along columnssum(A,1)A.sum(0)
sum along rowssum(A,2)A.sum(1)
product along columnsprod(A,1)
product along rowsprod(A,2)
cumsum along columnscumsum(A,1)A.cumsum(0)
cumsum along rowscumsum(A,2)A.cumsum(1)
cumproduct along columnscumprod(A,1)A.cumprod(0)
cumproduct along rowscumprod(A,2)A.cumprod(1)
number of rowssize(A,1)A.rows()
number of columnssize(A,2)A.cols()

Notes: *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

conjugate tranposeA'A.hermitian()