1 2 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 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261
|
@c Copyright (C) 1996-2025 The Octave Project Developers
@c
@c This file is part of Octave.
@c
@c Octave is free software: you can redistribute it and/or modify it
@c under the terms of the GNU General Public License as published by
@c the Free Software Foundation, either version 3 of the License, or
@c (at your option) any later version.
@c
@c Octave is distributed in the hope that it will be useful, but
@c WITHOUT ANY WARRANTY; without even the implied warranty of
@c MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
@c GNU General Public License for more details.
@c
@c You should have received a copy of the GNU General Public License
@c along with Octave; see the file COPYING. If not, see
@c <https://www.gnu.org/licenses/>.
@node Matrix Manipulation
@chapter Matrix Manipulation
There are a number of functions available for checking to see if the
elements of a matrix meet some condition, and for rearranging the
elements of a matrix. For example, Octave can easily tell you if all
the elements of a matrix are finite, or are less than some specified
value. Octave can also rotate the elements, extract the upper- or
lower-triangular parts, or sort the columns of a matrix.
@menu
* Finding Elements and Checking Conditions::
* Rearranging Matrices::
* Special Utility Matrices::
* Famous Matrices::
@end menu
@node Finding Elements and Checking Conditions
@section Finding Elements and Checking Conditions
The functions @code{any} and @code{all} are useful for determining
whether any or all of the elements of a matrix satisfy some condition.
The @code{find} function is also useful in determining which elements of
a matrix meet a specified condition.
@DOCSTRING(any)
@DOCSTRING(all)
Since the comparison operators (@pxref{Comparison Ops}) return matrices
of ones and zeros, it is easy to test a matrix for many things, not just
whether the elements are nonzero. For example,
@example
@group
all (all (rand (5) < 0.9))
@result{} 0
@end group
@end example
@noindent
tests a random 5 by 5 matrix to see if all of its elements are less
than 0.9.
Note that in conditional contexts (like the test clause of @code{if} and
@code{while} statements) Octave treats the test as if you had typed
@code{all (all (condition))}.
@DOCSTRING(xor)
@DOCSTRING(diff)
@DOCSTRING(isinf)
@DOCSTRING(isnan)
@DOCSTRING(isfinite)
@DOCSTRING(common_size)
@DOCSTRING(find)
@DOCSTRING(lookup)
If you wish to check if a variable exists at all, instead of properties
its elements may have, consult @ref{Status of Variables}.
@node Rearranging Matrices
@section Rearranging Matrices
@DOCSTRING(fliplr)
@DOCSTRING(flipud)
@DOCSTRING(flip)
@DOCSTRING(rot90)
@DOCSTRING(rotdim)
@DOCSTRING(cat)
@DOCSTRING(horzcat)
@DOCSTRING(vertcat)
@DOCSTRING(permute)
@DOCSTRING(ipermute)
@DOCSTRING(reshape)
@DOCSTRING(resize)
@DOCSTRING(circshift)
@DOCSTRING(shiftdim)
@DOCSTRING(sort)
@DOCSTRING(sortrows)
@DOCSTRING(issorted)
@DOCSTRING(nth_element)
@DOCSTRING(tril)
@DOCSTRING(triu)
@DOCSTRING(vec)
@DOCSTRING(vech)
@DOCSTRING(prepad)
@DOCSTRING(postpad)
@DOCSTRING(diag)
@DOCSTRING(blkdiag)
@node Special Utility Matrices
@section Special Utility Matrices
@DOCSTRING(eye)
@DOCSTRING(ones)
@DOCSTRING(zeros)
@DOCSTRING(repmat)
@DOCSTRING(repelems)
@DOCSTRING(repelem)
The functions @code{linspace} and @code{logspace} make it very easy to
create vectors with evenly or logarithmically spaced elements.
@xref{Ranges}.
@DOCSTRING(linspace)
@DOCSTRING(logspace)
@DOCSTRING(rand)
@DOCSTRING(randi)
@DOCSTRING(randn)
@DOCSTRING(rande)
@DOCSTRING(randp)
@DOCSTRING(randg)
@DOCSTRING(rng)
The generators operate in the new or old style together, it is not
possible to mix the two. Initializing any generator with
@qcode{"state"} or @qcode{"seed"} causes the others to switch to the
same style for future calls.
The state of each generator is independent and calls to different
generators can be interleaved without affecting the final result. For
example,
@example
@group
rand ("state", [11, 22, 33]);
randn ("state", [44, 55, 66]);
u = rand (100, 1);
n = randn (100, 1);
@end group
@end example
@noindent
and
@example
@group
rand ("state", [11, 22, 33]);
randn ("state", [44, 55, 66]);
u = zeros (100, 1);
n = zeros (100, 1);
for i = 1:100
u(i) = rand ();
n(i) = randn ();
end
@end group
@end example
@noindent
produce equivalent results. When the generators are initialized in
the old style with @qcode{"seed"} only @code{rand} and @code{randn} are
independent, because the old @code{rande}, @code{randg} and
@code{randp} generators make calls to @code{rand} and @code{randn}.
The generators are initialized with random states at start-up, so
that the sequences of random numbers are not the same each time you run
Octave.@footnote{The old versions of @code{rand} and @code{randn}
obtain their initial seeds from the system clock.} If you really do
need to reproduce a sequence of numbers exactly, you can set the state
or seed to a specific value.
If invoked without arguments, @code{rand} and @code{randn} return a
single element of a random sequence.
The original @code{rand} and @code{randn} functions use Fortran code from
@sc{ranlib}, a library of Fortran routines for random number generation,
compiled by Barry W. Brown and @nospell{James Lovato} of the Department of
Biomathematics at The University of Texas, M.D. Anderson Cancer Center,
Houston, TX 77030.
@DOCSTRING(randperm)
@node Famous Matrices
@section Famous Matrices
The following functions return famous matrix forms.
@DOCSTRING(gallery)
@DOCSTRING(hadamard)
@DOCSTRING(hankel)
@DOCSTRING(hilb)
@DOCSTRING(invhilb)
@DOCSTRING(magic)
@DOCSTRING(pascal)
@DOCSTRING(rosser)
@DOCSTRING(toeplitz)
@DOCSTRING(vander)
@DOCSTRING(wilkinson)
|