Next: , Previous: , Up: Diagonal and Permutation Matrices   [Contents][Index]


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;