<html lang="en"> <head> <title>Creating Permutation Matrices - GNU Octave</title> <meta http-equiv="Content-Type" content="text/html"> <meta name="description" content="GNU Octave"> <meta name="generator" content="makeinfo 4.13"> <link title="Top" rel="start" href="index.html#Top"> <link rel="up" href="Basic-Usage.html#Basic-Usage" title="Basic Usage"> <link rel="prev" href="Creating-Diagonal-Matrices.html#Creating-Diagonal-Matrices" title="Creating Diagonal Matrices"> <link rel="next" href="Explicit-and-Implicit-Conversions.html#Explicit-and-Implicit-Conversions" title="Explicit and Implicit Conversions"> <link href="http://www.gnu.org/software/texinfo/" rel="generator-home" title="Texinfo Homepage"> <meta http-equiv="Content-Style-Type" content="text/css"> <style type="text/css"><!-- pre.display { font-family:inherit } pre.format { font-family:inherit } pre.smalldisplay { font-family:inherit; font-size:smaller } pre.smallformat { font-family:inherit; font-size:smaller } pre.smallexample { font-size:smaller } pre.smalllisp { font-size:smaller } span.sc { font-variant:small-caps } span.roman { font-family:serif; font-weight:normal; } span.sansserif { font-family:sans-serif; font-weight:normal; } --></style> </head> <body> <div class="node"> <a name="Creating-Permutation-Matrices"></a> <p> Next: <a rel="next" accesskey="n" href="Explicit-and-Implicit-Conversions.html#Explicit-and-Implicit-Conversions">Explicit and Implicit Conversions</a>, Previous: <a rel="previous" accesskey="p" href="Creating-Diagonal-Matrices.html#Creating-Diagonal-Matrices">Creating Diagonal Matrices</a>, Up: <a rel="up" accesskey="u" href="Basic-Usage.html#Basic-Usage">Basic Usage</a> <hr> </div> <h4 class="subsection">21.1.2 Creating Permutation Matrices</h4> <p>For creating permutation matrices, Octave does not introduce a new function, but rather overrides an existing syntax: permutation matrices can be conveniently created by indexing an identity matrix by permutation vectors. That is, if <var>q</var> is a permutation vector of length <var>n</var>, the expression <pre class="example"> P = eye (n) (:, q); </pre> <p class="noindent">will create a permutation matrix - a special matrix object. <pre class="example"> eye (n) (q, :) </pre> <p class="noindent">will also work (and create a row permutation matrix), as well as <pre class="example"> eye (n) (q1, q2). </pre> <p>For example: <pre class="example"> eye (4) ([1,3,2,4],:) ⇒ Permutation Matrix 1 0 0 0 0 0 1 0 0 1 0 0 0 0 0 1 eye (4) (:,[1,3,2,4]) ⇒ Permutation Matrix 1 0 0 0 0 0 1 0 0 1 0 0 0 0 0 1 </pre> <p>Mathematically, an identity matrix is both diagonal and permutation matrix. In Octave, <code>eye (n)</code> returns a diagonal matrix, because a matrix can only have one class. You can convert this diagonal matrix to a permutation matrix by indexing it by an identity permutation, as shown below. This is a special property of the identity matrix; indexing other diagonal matrices generally produces a full matrix. <pre class="example"> eye (3) ⇒ Diagonal Matrix 1 0 0 0 1 0 0 0 1 eye(3)(1:3,:) ⇒ Permutation Matrix 1 0 0 0 1 0 0 0 1 </pre> <p>Some other built-in functions can also return permutation matrices. Examples include <dfn>inv</dfn> or <dfn>lu</dfn>. </body></html>