| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
 100
 
 | <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>
 |