W3cubDocs

/Octave

21.4 Examples of Usage

The following can be used to solve a linear system A*x = b using the pivoted LU factorization:

[L, U, P] = lu (A); ## now L*U = P*A
  x = U \ (L \ P) * b;

This is one way to normalize columns of a matrix X to unit norm:

s = norm (X, "columns");
  X /= diag (s);

The same can also be accomplished with broadcasting (see Broadcasting):

s = norm (X, "columns");
  X ./= s;

The following expression is a way to efficiently calculate the sign of a permutation, given by a permutation vector p. It will also work in earlier versions of Octave, but slowly.

det (eye (length (p))(p, :))

Finally, here’s how to solve a linear system A*x = b with Tikhonov regularization (ridge regression) using SVD (a skeleton only):

m = rows (A); n = columns (A);
  [U, S, V] = svd (A);
  ## determine the regularization factor alpha
  ## alpha = …
  ## transform to orthogonal basis
  b = U'*b;
  ## Use the standard formula, replacing A with S.
  ## S is diagonal, so the following will be very fast and accurate.
  x = (S'*S + alpha^2 * eye (n)) \ (S' * b);
  ## transform to solution basis
  x = V*x;

© 1996–2018 John W. Eaton
Permission is granted to make and distribute verbatim copies of this manual provided the copyright notice and this permission notice are preserved on all copies.
Permission is granted to copy and distribute modified versions of this manual under the conditions for verbatim copying, provided that the entire resulting derived work is distributed under the terms of a permission notice identical to this one.Permission is granted to copy and distribute translations of this manual into another language, under the above conditions for modified versions.
https://octave.org/doc/interpreter/Example-Code.html