File: octave_19.html

package info (click to toggle)
octave2.1 1%3A2.1.73-13
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 37,028 kB
  • ctags: 20,874
  • sloc: cpp: 106,508; fortran: 46,978; ansic: 5,720; sh: 4,800; makefile: 3,186; yacc: 3,132; lex: 2,892; lisp: 1,715; perl: 778; awk: 174; exp: 134
file content (1369 lines) | stat: -rw-r--r-- 44,803 bytes parent folder | download | duplicates (2)
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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
                      "http://www.w3.org/TR/html40/loose.dtd">
<HTML>
<!-- Created on May, 18 2005 by texi2html 1.66 -->
<!--
Written by: Lionel Cons <Lionel.Cons@cern.ch> (original author)
            Karl Berry  <karl@freefriends.org>
            Olaf Bachmann <obachman@mathematik.uni-kl.de>
            and many others.
Maintained by: Many creative people <dev@texi2html.cvshome.org>
Send bugs and suggestions to <users@texi2html.cvshome.org>

-->
<HEAD>
<TITLE>GNU Octave: Matrix Manipulation</TITLE>

<META NAME="description" CONTENT="GNU Octave: Matrix Manipulation">
<META NAME="keywords" CONTENT="GNU Octave: Matrix Manipulation">
<META NAME="resource-type" CONTENT="document">
<META NAME="distribution" CONTENT="global">
<META NAME="Generator" CONTENT="texi2html 1.66">

</HEAD>

<BODY LANG="en" BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#0000FF" VLINK="#800080" ALINK="#FF0000">

<A NAME="SEC147"></A>
<TABLE CELLPADDING=1 CELLSPACING=1 BORDER=0>
<TR><TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="octave_18.html#SEC146"> &lt; </A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="octave_19.html#SEC148"> &gt; </A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT"> &nbsp; <TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="octave_18.html#SEC138"> &lt;&lt; </A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="octave.html#SEC_Top"> Up </A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="octave_20.html#SEC152"> &gt;&gt; </A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT"> &nbsp; <TD VALIGN="MIDDLE" ALIGN="LEFT"> &nbsp; <TD VALIGN="MIDDLE" ALIGN="LEFT"> &nbsp; <TD VALIGN="MIDDLE" ALIGN="LEFT"> &nbsp; <TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="octave.html#SEC_Top">Top</A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="octave_toc.html#SEC_Contents">Contents</A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="octave_42.html#SEC245">Index</A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="octave_abt.html#SEC_About"> ? </A>]</TD>
</TR></TABLE>
<H1> 18. Matrix Manipulation </H1>
<!--docid::SEC147::-->
<P>

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.
</P>
<P>

<TABLE BORDER="0" CELLSPACING="0">
<TR><TD ALIGN="left" VALIGN="TOP"><A HREF="octave_19.html#SEC148">18.1 Finding Elements and Checking Conditions</A></TD><TD>&nbsp;&nbsp;</TD><TD ALIGN="left" VALIGN="TOP"></TD></TR>
<TR><TD ALIGN="left" VALIGN="TOP"><A HREF="octave_19.html#SEC149">18.2 Rearranging Matrices</A></TD><TD>&nbsp;&nbsp;</TD><TD ALIGN="left" VALIGN="TOP"></TD></TR>
<TR><TD ALIGN="left" VALIGN="TOP"><A HREF="octave_19.html#SEC150">18.3 Special Utility Matrices</A></TD><TD>&nbsp;&nbsp;</TD><TD ALIGN="left" VALIGN="TOP"></TD></TR>
<TR><TD ALIGN="left" VALIGN="TOP"><A HREF="octave_19.html#SEC151">18.4 Famous Matrices</A></TD><TD>&nbsp;&nbsp;</TD><TD ALIGN="left" VALIGN="TOP"></TD></TR>
</TABLE>
<P>

<A NAME="Finding Elements and Checking Conditions"></A>
<HR SIZE="6">
<A NAME="SEC148"></A>
<TABLE CELLPADDING=1 CELLSPACING=1 BORDER=0>
<TR><TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="octave_19.html#SEC147"> &lt; </A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="octave_19.html#SEC149"> &gt; </A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT"> &nbsp; <TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="octave_19.html#SEC147"> &lt;&lt; </A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="octave_19.html#SEC147"> Up </A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="octave_20.html#SEC152"> &gt;&gt; </A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT"> &nbsp; <TD VALIGN="MIDDLE" ALIGN="LEFT"> &nbsp; <TD VALIGN="MIDDLE" ALIGN="LEFT"> &nbsp; <TD VALIGN="MIDDLE" ALIGN="LEFT"> &nbsp; <TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="octave.html#SEC_Top">Top</A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="octave_toc.html#SEC_Contents">Contents</A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="octave_42.html#SEC245">Index</A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="octave_abt.html#SEC_About"> ? </A>]</TD>
</TR></TABLE>
<H2> 18.1 Finding Elements and Checking Conditions </H2>
<!--docid::SEC148::-->
<P>

The functions <CODE>any</CODE> and <CODE>all</CODE> are useful for determining
whether any or all of the elements of a matrix satisfy some condition.
The <CODE>find</CODE> function is also useful in determining which elements of
a matrix meet a specified condition.
</P>
<P>

<A NAME="doc-any"></A>
<A NAME="IDX539"></A>
</P>
<DL>
<DT><U>Built-in Function:</U>  <B>any</B> <I>(<VAR>x</VAR>, <VAR>dim</VAR>)</I>
<DD>For a vector argument, return 1 if any element of the vector is
nonzero.
<P>

For a matrix argument, return a row vector of ones and
zeros with each element indicating whether any of the elements of the
corresponding column of the matrix are nonzero.  For example,
</P>
<P>

<TABLE><tr><td>&nbsp;</td><td class=example><pre>any (eye (2, 4))
     => [ 1, 1, 0, 0 ]
</pre></td></tr></table><P>

If the optional argument <VAR>dim</VAR> is supplied, work along dimension
<VAR>dim</VAR>.  For example,
</P>
<P>

<TABLE><tr><td>&nbsp;</td><td class=example><pre>any (eye (2, 4), 2)
     => [ 1; 1 ]
</pre></td></tr></table></DL>
<P>

<A NAME="doc-all"></A>
<A NAME="IDX540"></A>
</P>
<DL>
<DT><U>Built-in Function:</U>  <B>all</B> <I>(<VAR>x</VAR>, <VAR>dim</VAR>)</I>
<DD>The function <CODE>all</CODE> behaves like the function <CODE>any</CODE>, except
that it returns true only if all the elements of a vector, or all the
elements along dimension <VAR>dim</VAR> of a matrix, are nonzero.
</DL>
<P>

Since the comparison operators (see section <A HREF="octave_11.html#SEC78">10.4 Comparison Operators</A>) 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, 
</P>
<P>

<TABLE><tr><td>&nbsp;</td><td class=example><pre>all (all (rand (5) &lt; 0.9))
     => 0
</pre></td></tr></table><P>

tests a random 5 by 5 matrix to see if all of its elements are less
than 0.9.
</P>
<P>

Note that in conditional contexts (like the test clause of <CODE>if</CODE> and
<CODE>while</CODE> statements) Octave treats the test as if you had typed
<CODE>all (all (condition))</CODE>.
</P>
<P>

<A NAME="doc-xor"></A>
<A NAME="IDX541"></A>
</P>
<DL>
<DT><U>Mapping Function:</U>  <B>xor</B> <I>(<VAR>x</VAR>, <VAR>y</VAR>)</I>
<DD>Return the `exclusive or' of the entries of <VAR>x</VAR> and <VAR>y</VAR>.
For boolean expressions <VAR>x</VAR> and <VAR>y</VAR>,
<CODE>xor (<VAR>x</VAR>, <VAR>y</VAR>)</CODE> is true if and only if <VAR>x</VAR> or <VAR>y</VAR>
is true, but not if both <VAR>x</VAR> and <VAR>y</VAR> are true.
</DL>
<P>

<A NAME="doc-is_duplicate_entry"></A>
<A NAME="IDX542"></A>
</P>
<DL>
<DT><U>Function File:</U>  <B>is_duplicate_entry</B> <I>(<VAR>x</VAR>)</I>
<DD>Return non-zero if any entries in <VAR>x</VAR> are duplicates of one
another.
</DL>
<P>

<A NAME="doc-diff"></A>
<A NAME="IDX543"></A>
</P>
<DL>
<DT><U>Function File:</U>  <B>diff</B> <I>(<VAR>x</VAR>, <VAR>k</VAR>, <VAR>dim</VAR>)</I>
<DD>If <VAR>x</VAR> is a vector of length <VAR>n</VAR>, <CODE>diff (<VAR>x</VAR>)</CODE> is the
vector of first differences
<VAR>x</VAR>(2) - <VAR>x</VAR>(1), <small>...</small>, <VAR>x</VAR>(n) - <VAR>x</VAR>(n-1).
<P>

If <VAR>x</VAR> is a matrix, <CODE>diff (<VAR>x</VAR>)</CODE> is the matrix of column
differences along the first non-singleton dimension.
</P>
<P>

The second argument is optional.  If supplied, <CODE>diff (<VAR>x</VAR>,
<VAR>k</VAR>)</CODE>, where <VAR>k</VAR> is a nonnegative integer, returns the
<VAR>k</VAR>-th differences. It is possible that <VAR>k</VAR> is larger than
then first non-singleton dimension of the matrix. In this case,
<CODE>diff</CODE> continues to take the differences along the next
non-singleton dimension.
</P>
<P>

The dimension along which to take the difference can be explicitly
stated with the optional variable <VAR>dim</VAR>. In this case the 
<VAR>k</VAR>-th order differences are calculated along this dimension.
In the case where <VAR>k</VAR> exceeds <CODE>size (<VAR>x</VAR>, <VAR>dim</VAR>)</CODE>
then an empty matrix is returned.
</P>
</DL>
<P>

<A NAME="doc-isinf"></A>
<A NAME="IDX544"></A>
</P>
<DL>
<DT><U>Mapping Function:</U>  <B>isinf</B> <I>(<VAR>x</VAR>)</I>
<DD>Return 1 for elements of <VAR>x</VAR> that are infinite and zero
otherwise. For example,
<P>

<TABLE><tr><td>&nbsp;</td><td class=example><pre>isinf ([13, Inf, NA, NaN])
     => [ 0, 1, 0, 0 ]
</pre></td></tr></table></DL>
<P>

<A NAME="doc-isnan"></A>
<A NAME="IDX545"></A>
</P>
<DL>
<DT><U>Mapping Function:</U>  <B>isnan</B> <I>(<VAR>x</VAR>)</I>
<DD>Return 1 for elements of <VAR>x</VAR> that are NaN values and zero
otherwise. For example,
<P>

<TABLE><tr><td>&nbsp;</td><td class=example><pre>isnan ([13, Inf, NA, NaN])
     => [ 0, 0, 0, 1 ]
</pre></td></tr></table></DL>
<P>

<A NAME="doc-finite"></A>
<A NAME="IDX546"></A>
</P>
<DL>
<DT><U>Mapping Function:</U>  <B>finite</B> <I>(<VAR>x</VAR>)</I>
<DD>Return 1 for elements of <VAR>x</VAR> that are finite values and zero
otherwise. For example,
<P>

<TABLE><tr><td>&nbsp;</td><td class=example><pre>finite ([13, Inf, NA, NaN])
     => [ 1, 0, 0, 0 ]
</pre></td></tr></table></DL>
<P>

<A NAME="doc-find"></A>
<A NAME="IDX547"></A>
</P>
<DL>
<DT><U>Loadable Function:</U>  <B>find</B> <I>(<VAR>x</VAR>)</I>
<DD>Return a vector of indices of nonzero elements of a matrix.  To obtain a
single index for each matrix element, Octave pretends that the columns
of a matrix form one long vector (like Fortran arrays are stored).  For
example,
<P>

<TABLE><tr><td>&nbsp;</td><td class=example><pre>find (eye (2))
     => [ 1; 4 ]
</pre></td></tr></table><P>

If two outputs are requested, <CODE>find</CODE> returns the row and column
indices of nonzero elements of a matrix.  For example,
</P>
<P>

<TABLE><tr><td>&nbsp;</td><td class=example><pre>[i, j] = find (2 * eye (2))
     => i = [ 1; 2 ]
     => j = [ 1; 2 ]
</pre></td></tr></table><P>

If three outputs are requested, <CODE>find</CODE> also returns a vector
containing the nonzero values.  For example,
</P>
<P>

<TABLE><tr><td>&nbsp;</td><td class=example><pre>[i, j, v] = find (3 * eye (2))
     => i = [ 1; 2 ]
     => j = [ 1; 2 ]
     => v = [ 3; 3 ]
</pre></td></tr></table></DL>
<P>

        
<A NAME="doc-common_size"></A>
<A NAME="IDX548"></A>
</P>
<DL>
<DT><U>Function File:</U> [<VAR>err</VAR>, <VAR>y1</VAR>, ...] = <B>common_size</B> <I>(<VAR>x1</VAR>, ...)</I>
<DD>Determine if all input arguments are either scalar or of common
size.  If so, <VAR>err</VAR> is zero, and <VAR>yi</VAR> is a matrix of the
common size with all entries equal to <VAR>xi</VAR> if this is a scalar or
<VAR>xi</VAR> otherwise.  If the inputs cannot be brought to a common size,
errorcode is 1, and <VAR>yi</VAR> is <VAR>xi</VAR>.  For example,
<P>

<TABLE><tr><td>&nbsp;</td><td class=example><pre>[errorcode, a, b] = common_size ([1 2; 3 4], 5)
=> errorcode = 0
=> a = [ 1, 2; 3, 4 ]
=> b = [ 5, 5; 5, 5 ]
</pre></td></tr></table><P>

This is useful for implementing functions where arguments can either
be scalars or of common size.
</P>
</DL>
<P>

<A NAME="Rearranging Matrices"></A>
<HR SIZE="6">
<A NAME="SEC149"></A>
<TABLE CELLPADDING=1 CELLSPACING=1 BORDER=0>
<TR><TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="octave_19.html#SEC148"> &lt; </A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="octave_19.html#SEC150"> &gt; </A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT"> &nbsp; <TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="octave_19.html#SEC147"> &lt;&lt; </A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="octave_19.html#SEC147"> Up </A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="octave_20.html#SEC152"> &gt;&gt; </A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT"> &nbsp; <TD VALIGN="MIDDLE" ALIGN="LEFT"> &nbsp; <TD VALIGN="MIDDLE" ALIGN="LEFT"> &nbsp; <TD VALIGN="MIDDLE" ALIGN="LEFT"> &nbsp; <TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="octave.html#SEC_Top">Top</A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="octave_toc.html#SEC_Contents">Contents</A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="octave_42.html#SEC245">Index</A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="octave_abt.html#SEC_About"> ? </A>]</TD>
</TR></TABLE>
<H2> 18.2 Rearranging Matrices </H2>
<!--docid::SEC149::-->
<P>

<A NAME="doc-fliplr"></A>
<A NAME="IDX549"></A>
</P>
<DL>
<DT><U>Function File:</U>  <B>fliplr</B> <I>(<VAR>x</VAR>)</I>
<DD>Return a copy of <VAR>x</VAR> with the order of the columns reversed.  For
example,
<P>

<TABLE><tr><td>&nbsp;</td><td class=example><pre>fliplr ([1, 2; 3, 4])
=>  2  1
         4  3
</pre></td></tr></table><P>

Note that <CODE>fliplr</CODE> only workw with 2-D arrays.  To flip N-d arrays
use <CODE>flipdim</CODE> instead.
</P>
</DL>
<P>

<A NAME="doc-flipud"></A>
<A NAME="IDX550"></A>
</P>
<DL>
<DT><U>Function File:</U>  <B>flipud</B> <I>(<VAR>x</VAR>)</I>
<DD>Return a copy of <VAR>x</VAR> with the order of the rows reversed.  For
example,
<P>

<TABLE><tr><td>&nbsp;</td><td class=example><pre>flipud ([1, 2; 3, 4])
=>  3  4
         1  2
</pre></td></tr></table><P>

Due to the difficulty of defining which axis about which to flip the 
matrix <CODE>flipud</CODE> only work with 2-d arrays.  To flip N-d arrays
use <CODE>flipdim</CODE> instead.
</P>
</DL>
<P>

<A NAME="doc-flipdim"></A>
<A NAME="IDX551"></A>
</P>
<DL>
<DT><U>Function File:</U>  <B>flipdim</B> <I>(<VAR>x</VAR>, <VAR>dim</VAR>)</I>
<DD>Return a copy of <VAR>x</VAR> flipped about the dimension <VAR>dim</VAR>.
For example
<P>

<TABLE><tr><td>&nbsp;</td><td class=example><pre>flipdim ([1, 2; 3, 4], 2)
=>  2  1
         4  3
</pre></td></tr></table></DL>
<P>

<A NAME="doc-rot90"></A>
<A NAME="IDX552"></A>
</P>
<DL>
<DT><U>Function File:</U>  <B>rot90</B> <I>(<VAR>x</VAR>, <VAR>n</VAR>)</I>
<DD>Return a copy of <VAR>x</VAR> with the elements rotated counterclockwise in
90-degree increments.  The second argument is optional, and specifies
how many 90-degree rotations are to be applied (the default value is 1).
Negative values of <VAR>n</VAR> rotate the matrix in a clockwise direction.
For example,
<P>

<TABLE><tr><td>&nbsp;</td><td class=example><pre>rot90 ([1, 2; 3, 4], -1)
=>  3  1
         4  2
</pre></td></tr></table><P>

rotates the given matrix clockwise by 90 degrees.  The following are all
equivalent statements:
</P>
<P>

<TABLE><tr><td>&nbsp;</td><td class=example><pre>rot90 ([1, 2; 3, 4], -1)
==
rot90 ([1, 2; 3, 4], 3)
==
rot90 ([1, 2; 3, 4], 7)
</pre></td></tr></table><P>

Due to the difficulty of defining an axis about which to rotate the 
matrix <CODE>rot90</CODE> only work with 2-D arrays.  To rotate N-d arrays
use <CODE>rotdim</CODE> instead.
</P>
</DL>
<P>

<A NAME="doc-rotdim"></A>
<A NAME="IDX553"></A>
</P>
<DL>
<DT><U>Function File:</U>  <B>rotdim</B> <I>(<VAR>x</VAR>, <VAR>n</VAR>, <VAR>plane</VAR>)</I>
<DD>Return a copy of <VAR>x</VAR> with the elements rotated counterclockwise in
90-degree increments.  The second argument is optional, and specifies
how many 90-degree rotations are to be applied (the default value is 1).
The third argument is also optional and defines the plane of the
rotation. As such <VAR>plane</VAR> is a two element vector containing two
different valid dimensions of the matrix. If <VAR>plane</VAR> is not given
Then the first two non-singleton dimensions are used.
<P>

Negative values of <VAR>n</VAR> rotate the matrix in a clockwise direction.
For example,
</P>
<P>

<TABLE><tr><td>&nbsp;</td><td class=example><pre>rotdim ([1, 2; 3, 4], -1, [1, 2])
=>  3  1
         4  2
</pre></td></tr></table><P>

rotates the given matrix clockwise by 90 degrees.  The following are all
equivalent statements:
</P>
<P>

<TABLE><tr><td>&nbsp;</td><td class=example><pre>rot90 ([1, 2; 3, 4], -1, [1, 2])
==
rot90 ([1, 2; 3, 4], 3, [1, 2])
==
rot90 ([1, 2; 3, 4], 7, [1, 2])
</pre></td></tr></table></DL>
<P>

<A NAME="doc-cat"></A>
<A NAME="IDX554"></A>
</P>
<DL>
<DT><U>Built-in Function:</U>  <B>cat</B> <I>(<VAR>dim</VAR>, <VAR>array1</VAR>, <VAR>array2</VAR>, <small>...</small>, <VAR>arrayN</VAR>)</I>
<DD>Return the concatenation of N-d array objects, <VAR>array1</VAR>,
<VAR>array2</VAR>, <small>...</small>, <VAR>arrayN</VAR> along dimension <VAR>dim</VAR>.
<P>

<TABLE><tr><td>&nbsp;</td><td class=example><pre>A = ones (2, 2);
B = zeros (2, 2);
cat (2, A, B)
=> ans =

     1 1 0 0
     1 1 0 0
</pre></td></tr></table><P>

Alternatively, we can concatenate <VAR>A</VAR> and <VAR>B</VAR> along the
second dimension the following way:
</P>
<P>

<TABLE><tr><td>&nbsp;</td><td class=example><pre>[A, B].
</pre></td></tr></table><P>

<VAR>dim</VAR> can be larger than the dimensions of the N-d array objects
and the result will thus have <VAR>dim</VAR> dimensions as the
following example shows:
<TABLE><tr><td>&nbsp;</td><td class=example><pre>cat (4, ones(2, 2), zeros (2, 2))
=> ans =

   ans(:,:,1,1) =

     1 1
     1 1

   ans(:,:,1,2) =
     0 0
     0 0
</pre></td></tr></table><P>

</P>
</DL>
<P>

<A NAME="doc-horzcat"></A>
<A NAME="IDX555"></A>
</P>
<DL>
<DT><U>Built-in Function:</U>  <B>horzcat</B> <I>(<VAR>array1</VAR>, <VAR>array2</VAR>, <small>...</small>, <VAR>arrayN</VAR>)</I>
<DD>Return the horizontal concatenation of N-d array objects, <VAR>array1</VAR>,
<VAR>array2</VAR>, <small>...</small>, <VAR>arrayN</VAR> along dimension 2.
</DL>
<P>

<A NAME="doc-vertcat"></A>
<A NAME="IDX556"></A>
</P>
<DL>
<DT><U>Built-in Function:</U>  <B>vertcat</B> <I>(<VAR>array1</VAR>, <VAR>array2</VAR>, <small>...</small>, <VAR>arrayN</VAR>)</I>
<DD>Return the vertical concatenation of N-d array objects, <VAR>array1</VAR>,
<VAR>array2</VAR>, <small>...</small>, <VAR>arrayN</VAR> along dimension 1.
</DL>
<P>

<A NAME="doc-permute"></A>
<A NAME="IDX557"></A>
</P>
<DL>
<DT><U>Built-in Function:</U>  <B>permute</B> <I>(<VAR>a</VAR>, <VAR>perm</VAR>)</I>
<DD>Return the generalized transpose for an N-d array object <VAR>a</VAR>.
The permutation vector <VAR>perm</VAR> must contain the elements
<CODE>1:ndims(a)</CODE> (in any order, but each element must appear just once).
<P>

</P>
</DL>
<P>

<A NAME="doc-ipermute"></A>
<A NAME="IDX558"></A>
</P>
<DL>
<DT><U>Built-in Function:</U>  <B>ipermute</B> <I>(<VAR>a</VAR>, <VAR>iperm</VAR>)</I>
<DD>The inverse of the <CODE>permute</CODE> function.  The expression
<P>

<TABLE><tr><td>&nbsp;</td><td class=example><pre>ipermute (permute (a, perm), perm)
</pre></td></tr></table>returns the original array <VAR>a</VAR>.
<P>

</P>
</DL>
<P>

<A NAME="doc-reshape"></A>
<A NAME="IDX559"></A>
</P>
<DL>
<DT><U>Function File:</U>  <B>reshape</B> <I>(<VAR>a</VAR>, <VAR>m</VAR>, <VAR>n</VAR>, <small>...</small>)</I>
<DD><A NAME="IDX560"></A>
<DT><U>Function File:</U>  <B>reshape</B> <I>(<VAR>a</VAR>, <VAR>siz</VAR>)</I>
<DD>Return a matrix with the given dimensions whose elements are taken
from the matrix <VAR>a</VAR>.  The elements of the matrix are access in
column-major order (like Fortran arrays are stored).
<P>

For example,
</P>
<P>

<TABLE><tr><td>&nbsp;</td><td class=example><pre>reshape ([1, 2, 3, 4], 2, 2)
     =>  1  3
         2  4
</pre></td></tr></table><P>

Note that the total number of elements in the original
matrix must match the total number of elements in the new matrix.
</P>
<P>

A single dimension of the return matrix can be unknown and is flagged
by an empty argument.
</P>
</DL>
<P>

<A NAME="doc-circshift"></A>
<A NAME="IDX561"></A>
</P>
<DL>
<DT><U>Function File:</U> <VAR>y</VAR> <B>=</B> <I>circshift (<VAR>x</VAR>, <VAR>n</VAR>)</I>
<DD>Circularly shifts the values of the array <VAR>x</VAR>. <VAR>n</VAR> must be
a vector of integers no longer than the number of dimensions in 
<VAR>x</VAR>. The values of <VAR>n</VAR> can be either positive or negative,
which determines the direction in which the values or <VAR>x</VAR> are
shifted. If an element of <VAR>n</VAR> is zero, then the corresponding
dimension of <VAR>x</VAR> will not be shifted. For example
<P>

<TABLE><tr><td>&nbsp;</td><td class=example><pre>x = [1, 2, 3; 4, 5, 6, 7, 8, 9];
circshift (x, 1)
=>  7, 8, 9
    1, 2, 3
    4, 5, 6
circshift (x, -2)
=>  7, 8, 9
    1, 2, 3
    4, 5, 6
circshift (x, [0,1])
=>  3, 1, 2
    6, 4, 5
    9, 7, 8
</pre></td></tr></table></DL>
<P>

<A NAME="doc-shiftdim"></A>
<A NAME="IDX562"></A>
</P>
<DL>
<DT><U>Function File:</U> <VAR>y</VAR> <B>=</B> <I>shiftdim (<VAR>x</VAR>, <VAR>n</VAR>)</I>
<DD><A NAME="IDX563"></A>
<DT><U>Function File:</U> [<VAR>y</VAR>, <VAR>ns</VAR>] <B>=</B> <I>shiftdim (<VAR>x</VAR>)</I>
<DD>Shifts the dimension of <VAR>x</VAR> by <VAR>n</VAR>, where <VAR>n</VAR> must be
an integer scalar. When <VAR>n</VAR> is negative, the dimensions of
<VAR>x</VAR> are shifted to the left, with the leading dimensions
circulated to the end. If <VAR>n</VAR> is positive, then the dimensions
of <VAR>x</VAR> are shifted to the right, with the <VAR>n</VAR> singleton
dimensions added.
<P>

Called with a single argument, <CODE>shiftdim</CODE>, removes the leading
singleton dimensions, returning the number of dimensions removed
in the second output argument <VAR>ns</VAR>.
</P>
<P>

For example 
</P>
<P>

<TABLE><tr><td>&nbsp;</td><td class=example><pre>x = ones (1, 2, 3);
size (shiftdim (x, -1))
=> [2, 3, 1]
size (shiftdim (x, 1))
=> [1, 1, 2, 3]
[b, ns] = shiftdim (x);
=> b =  [1, 1, 1; 1, 1, 1]
=> ns = 1
</pre></td></tr></table></DL>
<P>

<A NAME="doc-shift"></A>
<A NAME="IDX564"></A>
</P>
<DL>
<DT><U>Function File:</U>  <B>shift</B> <I>(<VAR>x</VAR>, <VAR>b</VAR>)</I>
<DD><A NAME="IDX565"></A>
<DT><U>Function File:</U>  <B>shift</B> <I>(<VAR>x</VAR>, <VAR>b</VAR>, <VAR>dim</VAR>)</I>
<DD>If <VAR>x</VAR> is a vector, perform a circular shift of length <VAR>b</VAR> of
the elements of <VAR>x</VAR>.
<P>

If <VAR>x</VAR> is a matrix, do the same for each column of <VAR>x</VAR>.
If the optional <VAR>dim</VAR> argument is given, operate along this 
dimension
</P>
</DL>
<P>

<A NAME="doc-sort"></A>
<A NAME="IDX566"></A>
</P>
<DL>
<DT><U>Loadable Function:</U> [<VAR>s</VAR>, <VAR>i</VAR>] = <B>sort</B> <I>(<VAR>x</VAR>)</I>
<DD><A NAME="IDX567"></A>
<DT><U>Loadable Function:</U> [<VAR>s</VAR>, <VAR>i</VAR>] = <B>sort</B> <I>(<VAR>x</VAR>, <VAR>dim</VAR>)</I>
<DD><A NAME="IDX568"></A>
<DT><U>Loadable Function:</U> [<VAR>s</VAR>, <VAR>i</VAR>] = <B>sort</B> <I>(<VAR>x</VAR>, <VAR>mode</VAR>)</I>
<DD><A NAME="IDX569"></A>
<DT><U>Loadable Function:</U> [<VAR>s</VAR>, <VAR>i</VAR>] = <B>sort</B> <I>(<VAR>x</VAR>, <VAR>dim</VAR>, <VAR>mode</VAR>)</I>
<DD>Return a copy of <VAR>x</VAR> with the elements elements arranged in
increasing order.  For matrices, <CODE>sort</CODE> orders the elements in each
column.
<P>

For example,
</P>
<P>

<TABLE><tr><td>&nbsp;</td><td class=example><pre>sort ([1, 2; 2, 3; 3, 1])
     =>  1  1
         2  2
         3  3
</pre></td></tr></table><P>

The <CODE>sort</CODE> function may also be used to produce a matrix
containing the original row indices of the elements in the sorted
matrix.  For example,
</P>
<P>

<TABLE><tr><td>&nbsp;</td><td class=example><pre>[s, i] = sort ([1, 2; 2, 3; 3, 1])
     => s = 1  1
            2  2
            3  3
     => i = 1  3
            2  1
            3  2
</pre></td></tr></table><P>

If the optional argument <VAR>dim</VAR> is given, then the matrix is sorted
along the dimension defined by <VAR>dim</VAR>. The optional argument <CODE>mode</CODE>
defines the order in which the values will be sorted. Valid values of
<CODE>mode</CODE> are `ascend' or `descend'.
</P>
<P>

For equal elements, the indices are such that the equal elements are listed
in the order that appeared in the original list.
</P>
<P>

The <CODE>sort</CODE> function may also be used to sort strings and cell arrays
of strings, it which case the dictionary order of the strings is used.
</P>
<P>

The algorithm used in <CODE>sort</CODE> is optimized for the sorting of partially
ordered lists.
</P>
</DL>
<P>

Since the <CODE>sort</CODE> function does not allow sort keys to be specified,
it can't be used to order the rows of a matrix according to the values
of the elements in various columns<A NAME="DOCF5" HREF="octave_fot.html#FOOT5">(5)</A>
in a single call.  Using the second output, however, it is possible to
sort all rows based on the values in a given column.  Here's an example
that sorts the rows of a matrix based on the values in the second
column.
</P>
<P>

<TABLE><tr><td>&nbsp;</td><td class=example><pre>a = [1, 2; 2, 3; 3, 1];
[s, i] = sort (a (:, 2));
a (i, :)
     =>  3  1
         1  2
         2  3
</pre></td></tr></table><P>

<A NAME="doc-tril"></A>
<A NAME="IDX570"></A>
</P>
<DL>
<DT><U>Function File:</U>  <B>tril</B> <I>(<VAR>a</VAR>, <VAR>k</VAR>)</I>
<DD><A NAME="IDX571"></A>
<DT><U>Function File:</U>  <B>triu</B> <I>(<VAR>a</VAR>, <VAR>k</VAR>)</I>
<DD>Return a new matrix formed by extracting extract the lower (<CODE>tril</CODE>)
or upper (<CODE>triu</CODE>) triangular part of the matrix <VAR>a</VAR>, and
setting all other elements to zero.  The second argument is optional,
and specifies how many diagonals above or below the main diagonal should
also be set to zero.
<P>

The default value of <VAR>k</VAR> is zero, so that <CODE>triu</CODE> and
<CODE>tril</CODE> normally include the main diagonal as part of the result
matrix.
</P>
<P>

If the value of <VAR>k</VAR> is negative, additional elements above (for
<CODE>tril</CODE>) or below (for <CODE>triu</CODE>) the main diagonal are also
selected.
</P>
<P>

The absolute value of <VAR>k</VAR> must not be greater than the number of
sub- or super-diagonals.
</P>
<P>

For example,
</P>
<P>

<TABLE><tr><td>&nbsp;</td><td class=example><pre>tril (ones (3), -1)
=>  0  0  0
         1  0  0
         1  1  0
</pre></td></tr></table><P>

and
</P>
<P>

<TABLE><tr><td>&nbsp;</td><td class=example><pre>tril (ones (3), 1)
=>  1  1  0
         1  1  1
         1  1  1
</pre></td></tr></table></DL>
<P>

<A NAME="doc-vec"></A>
<A NAME="IDX572"></A>
</P>
<DL>
<DT><U>Function File:</U>  <B>vec</B> <I>(<VAR>x</VAR>)</I>
<DD>Return the vector obtained by stacking the columns of the matrix <VAR>x</VAR>
one above the other.
</DL>
<P>

<A NAME="doc-vech"></A>
<A NAME="IDX573"></A>
</P>
<DL>
<DT><U>Function File:</U>  <B>vech</B> <I>(<VAR>x</VAR>)</I>
<DD>Return the vector obtained by eliminating all supradiagonal elements of
the square matrix <VAR>x</VAR> and stacking the result one column above the
other.
</DL>
<P>

<A NAME="doc-prepad"></A>
<A NAME="IDX574"></A>
</P>
<DL>
<DT><U>Function File:</U>  <B>prepad</B> <I>(<VAR>x</VAR>, <VAR>l</VAR>, <VAR>c</VAR>)</I>
<DD><A NAME="IDX575"></A>
<DT><U>Function File:</U>  <B>postpad</B> <I>(<VAR>x</VAR>, <VAR>l</VAR>, <VAR>c</VAR>)</I>
<DD><A NAME="IDX576"></A>
<DT><U>Function File:</U>  <B>postpad</B> <I>(<VAR>x</VAR>, <VAR>l</VAR>, <VAR>c</VAR>, <VAR>dim</VAR>)</I>
<DD><P>

Prepends (appends) the scalar value <VAR>c</VAR> to the vector <VAR>x</VAR>
until it is of length <VAR>l</VAR>.  If the third argument is not
supplied, a value of 0 is used.
</P>
<P>

If <CODE>length (<VAR>x</VAR>) &gt; <VAR>l</VAR></CODE>, elements from the beginning (end) of
<VAR>x</VAR> are removed until a vector of length <VAR>l</VAR> is obtained.
</P>
<P>

If <VAR>x</VAR> is a matrix, elements are prepended or removed from each row.
</P>
<P>

If the optional <VAR>dim</VAR> argument is given, then operate along this
dimension.
</P>
</DL>
<P>

<A NAME="Special Utility Matrices"></A>
<HR SIZE="6">
<A NAME="SEC150"></A>
<TABLE CELLPADDING=1 CELLSPACING=1 BORDER=0>
<TR><TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="octave_19.html#SEC149"> &lt; </A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="octave_19.html#SEC151"> &gt; </A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT"> &nbsp; <TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="octave_19.html#SEC147"> &lt;&lt; </A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="octave_19.html#SEC147"> Up </A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="octave_20.html#SEC152"> &gt;&gt; </A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT"> &nbsp; <TD VALIGN="MIDDLE" ALIGN="LEFT"> &nbsp; <TD VALIGN="MIDDLE" ALIGN="LEFT"> &nbsp; <TD VALIGN="MIDDLE" ALIGN="LEFT"> &nbsp; <TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="octave.html#SEC_Top">Top</A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="octave_toc.html#SEC_Contents">Contents</A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="octave_42.html#SEC245">Index</A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="octave_abt.html#SEC_About"> ? </A>]</TD>
</TR></TABLE>
<H2> 18.3 Special Utility Matrices </H2>
<!--docid::SEC150::-->
<P>

<A NAME="doc-eye"></A>
<A NAME="IDX577"></A>
</P>
<DL>
<DT><U>Built-in Function:</U>  <B>eye</B> <I>(<VAR>x</VAR>)</I>
<DD><A NAME="IDX578"></A>
<DT><U>Built-in Function:</U>  <B>eye</B> <I>(<VAR>n</VAR>, <VAR>m</VAR>)</I>
<DD><A NAME="IDX579"></A>
<DT><U>Built-in Function:</U>  <B>eye</B> <I>(<small>...</small>, <VAR>class</VAR>)</I>
<DD>Return an identity matrix.  If invoked with a single scalar argument,
<CODE>eye</CODE> returns a square matrix with the dimension specified.  If you
supply two scalar arguments, <CODE>eye</CODE> takes them to be the number of
rows and columns.  If given a vector with two elements, <CODE>eye</CODE> uses
the values of the elements as the number of rows and columns,
respectively.  For example,
<P>

<TABLE><tr><td>&nbsp;</td><td class=example><pre>eye (3)
     =>  1  0  0
         0  1  0
         0  0  1
</pre></td></tr></table><P>

The following expressions all produce the same result:
</P>
<P>

<TABLE><tr><td>&nbsp;</td><td class=example><pre>eye (2)
==
eye (2, 2)
==
eye (size ([1, 2; 3, 4])
</pre></td></tr></table><P>

The optional argument <VAR>class</VAR>, allows <CODE>eye</CODE> to return an array of
the specified type, like
</P>
<P>

<TABLE><tr><td>&nbsp;</td><td class=example><pre>val = zeros (n,m, &quot;uint8&quot;)
</pre></td></tr></table><P>

For compatibility with MATLAB, calling <CODE>eye</CODE> with no arguments
is equivalent to calling it with an argument of 1.
</P>
</DL>
<P>

<A NAME="doc-ones"></A>
<A NAME="IDX580"></A>
</P>
<DL>
<DT><U>Built-in Function:</U>  <B>ones</B> <I>(<VAR>x</VAR>)</I>
<DD><A NAME="IDX581"></A>
<DT><U>Built-in Function:</U>  <B>ones</B> <I>(<VAR>n</VAR>, <VAR>m</VAR>)</I>
<DD><A NAME="IDX582"></A>
<DT><U>Built-in Function:</U>  <B>ones</B> <I>(<VAR>n</VAR>, <VAR>m</VAR>, <VAR>k</VAR>, <small>...</small>)</I>
<DD><A NAME="IDX583"></A>
<DT><U>Built-in Function:</U>  <B>ones</B> <I>(<small>...</small>, <VAR>class</VAR>)</I>
<DD>Return a matrix or N-dimensional array whose elements are all 1.
The arguments are handled the same as the arguments for <CODE>eye</CODE>.
<P>

If you need to create a matrix whose values are all the same, you should
use an expression like
</P>
<P>

<TABLE><tr><td>&nbsp;</td><td class=example><pre>val_matrix = val * ones (n, m)
</pre></td></tr></table><P>

The optional argument <VAR>class</VAR>, allows <CODE>ones</CODE> to return an array of
the specified type, like
</P>
<P>

<TABLE><tr><td>&nbsp;</td><td class=example><pre>val = ones (n,m, &quot;uint8&quot;)
</pre></td></tr></table></DL>
<P>

<A NAME="doc-zeros"></A>
<A NAME="IDX584"></A>
</P>
<DL>
<DT><U>Built-in Function:</U>  <B>zeros</B> <I>(<VAR>x</VAR>)</I>
<DD><A NAME="IDX585"></A>
<DT><U>Built-in Function:</U>  <B>zeros</B> <I>(<VAR>n</VAR>, <VAR>m</VAR>)</I>
<DD><A NAME="IDX586"></A>
<DT><U>Built-in Function:</U>  <B>zeros</B> <I>(<VAR>n</VAR>, <VAR>m</VAR>, <VAR>k</VAR>, <small>...</small>)</I>
<DD><A NAME="IDX587"></A>
<DT><U>Built-in Function:</U>  <B>zeros</B> <I>(<small>...</small>, <VAR>class</VAR>)</I>
<DD>Return a matrix or N-dimensional array whose elements are all 0.
The arguments are handled the same as the arguments for <CODE>eye</CODE>.
<P>

The optional argument <VAR>class</VAR>, allows <CODE>zeros</CODE> to return an array of
the specified type, like
</P>
<P>

<TABLE><tr><td>&nbsp;</td><td class=example><pre>val = zeros (n,m, &quot;uint8&quot;)
</pre></td></tr></table></DL>
<P>

<A NAME="doc-repmat"></A>
<A NAME="IDX588"></A>
</P>
<DL>
<DT><U>Function File:</U>  <B>repmat</B> <I>(<VAR>A</VAR>, <VAR>m</VAR>, <VAR>n</VAR>)</I>
<DD><A NAME="IDX589"></A>
<DT><U>Function File:</U>  <B>repmat</B> <I>(<VAR>A</VAR>, [<VAR>m</VAR> <VAR>n</VAR>])</I>
<DD>Form a block matrix of size <VAR>m</VAR> by <VAR>n</VAR>, with a copy of matrix
<VAR>A</VAR> as each element.  If <VAR>n</VAR> is not specified, form an 
<VAR>m</VAR> by <VAR>m</VAR> block matrix.
</DL>
<P>

<A NAME="doc-rand"></A>
<A NAME="IDX590"></A>
</P>
<DL>
<DT><U>Loadable Function:</U>  <B>rand</B> <I>(<VAR>x</VAR>)</I>
<DD><A NAME="IDX591"></A>
<DT><U>Loadable Function:</U>  <B>rand</B> <I>(<VAR>n</VAR>, <VAR>m</VAR>)</I>
<DD><A NAME="IDX592"></A>
<DT><U>Loadable Function:</U>  <B>rand</B> <I>(<CODE>&quot;seed&quot;</CODE>, <VAR>x</VAR>)</I>
<DD>Return a matrix with random elements uniformly distributed on the
interval (0, 1).  The arguments are handled the same as the arguments
for <CODE>eye</CODE>.  In
addition, you can set the seed for the random number generator using the
form
<P>

<TABLE><tr><td>&nbsp;</td><td class=example><pre>rand (&quot;seed&quot;, <VAR>x</VAR>)
</pre></td></tr></table><P>

where <VAR>x</VAR> is a scalar value.  If called as
</P>
<P>

<TABLE><tr><td>&nbsp;</td><td class=example><pre>rand (&quot;seed&quot;)
</pre></td></tr></table><P>

<CODE>rand</CODE> returns the current value of the seed.
</P>
</DL>
<P>

<A NAME="doc-randn"></A>
<A NAME="IDX593"></A>
</P>
<DL>
<DT><U>Loadable Function:</U>  <B>randn</B> <I>(<VAR>x</VAR>)</I>
<DD><A NAME="IDX594"></A>
<DT><U>Loadable Function:</U>  <B>randn</B> <I>(<VAR>n</VAR>, <VAR>m</VAR>)</I>
<DD><A NAME="IDX595"></A>
<DT><U>Loadable Function:</U>  <B>randn</B> <I>(<CODE>&quot;seed&quot;</CODE>, <VAR>x</VAR>)</I>
<DD>Return a matrix with normally distributed random elements.  The
arguments are handled the same as the arguments for <CODE>eye</CODE>.  In
addition, you can set the seed for the random number generator using the
form
<P>

<TABLE><tr><td>&nbsp;</td><td class=example><pre>randn (&quot;seed&quot;, <VAR>x</VAR>)
</pre></td></tr></table><P>

where <VAR>x</VAR> is a scalar value.  If called as
</P>
<P>

<TABLE><tr><td>&nbsp;</td><td class=example><pre>randn (&quot;seed&quot;)
</pre></td></tr></table><P>

<CODE>randn</CODE> returns the current value of the seed.
</P>
</DL>
<P>

The <CODE>rand</CODE> and <CODE>randn</CODE> functions use separate generators.
This ensures that
</P>
<P>

<TABLE><tr><td>&nbsp;</td><td class=example><pre>rand (&quot;seed&quot;, 13);
randn (&quot;seed&quot;, 13);
u = rand (100, 1);
n = randn (100, 1);
</pre></td></tr></table><P>

and
</P>
<P>

<TABLE><tr><td>&nbsp;</td><td class=example><pre>rand (&quot;seed&quot;, 13);
randn (&quot;seed&quot;, 13);
u = zeros (100, 1);
n = zeros (100, 1);
for i = 1:100
  u(i) = rand ();
  n(i) = randn ();
end
</pre></td></tr></table><P>

produce equivalent results.
</P>
<P>

Normally, <CODE>rand</CODE> and <CODE>randn</CODE> obtain their initial
seeds from the system clock, so that the sequence of random numbers is
not the same each time you run Octave.  If you really do need for to
reproduce a sequence of numbers exactly, you can set the seed to a
specific value.
</P>
<P>

If it is invoked without arguments, <CODE>rand</CODE> and <CODE>randn</CODE> return a
single element of a random sequence.
</P>
<P>

The <CODE>rand</CODE> and <CODE>randn</CODE> functions use Fortran code from
RANLIB, a library of fortran routines for random number generation,
compiled by Barry W. Brown and James Lovato of the Department of
Biomathematics at The University of Texas, M.D. Anderson Cancer Center,
Houston, TX 77030.
</P>
<P>

<A NAME="doc-randperm"></A>
<A NAME="IDX596"></A>
</P>
<DL>
<DT><U>Function File:</U>  <B>randperm</B> <I>(<VAR>n</VAR>)</I>
<DD>Return a row vector containing a random permutation of the
integers from 1 to <VAR>n</VAR>.
</DL>
<P>

<A NAME="doc-diag"></A>
<A NAME="IDX597"></A>
</P>
<DL>
<DT><U>Built-in Function:</U>  <B>diag</B> <I>(<VAR>v</VAR>, <VAR>k</VAR>)</I>
<DD>Return a diagonal matrix with vector <VAR>v</VAR> on diagonal <VAR>k</VAR>.  The
second argument is optional.  If it is positive, the vector is placed on
the <VAR>k</VAR>-th super-diagonal.  If it is negative, it is placed on the
<VAR>-k</VAR>-th sub-diagonal.  The default value of <VAR>k</VAR> is 0, and the
vector is placed on the main diagonal.  For example,
<P>

<TABLE><tr><td>&nbsp;</td><td class=example><pre>diag ([1, 2, 3], 1)
     =>  0  1  0  0
         0  0  2  0
         0  0  0  3
         0  0  0  0
</pre></td></tr></table></DL>
<P>

The functions <CODE>linspace</CODE> and <CODE>logspace</CODE> make it very easy to
create vectors with evenly or logarithmically spaced elements.
See section <A HREF="octave_5.html#SEC53">4.2 Ranges</A>.
</P>
<P>

<A NAME="doc-linspace"></A>
<A NAME="IDX598"></A>
</P>
<DL>
<DT><U>Built-in Function:</U>  <B>linspace</B> <I>(<VAR>base</VAR>, <VAR>limit</VAR>, <VAR>n</VAR>)</I>
<DD>Return a row vector with <VAR>n</VAR> linearly spaced elements between
<VAR>base</VAR> and <VAR>limit</VAR>.  The number of elements, <VAR>n</VAR>, must be
greater than 1.  The <VAR>base</VAR> and <VAR>limit</VAR> are always included in
the range.  If <VAR>base</VAR> is greater than <VAR>limit</VAR>, the elements are
stored in decreasing order.  If the number of points is not specified, a
value of 100 is used.
<P>

The <CODE>linspace</CODE> function always returns a row vector.
</P>
</DL>
<P>

<A NAME="doc-logspace"></A>
<A NAME="IDX599"></A>
</P>
<DL>
<DT><U>Function File:</U>  <B>logspace</B> <I>(<VAR>base</VAR>, <VAR>limit</VAR>, <VAR>n</VAR>)</I>
<DD>Similar to <CODE>linspace</CODE> except that the values are logarithmically
spaced from
10^base to 10^limit.
<P>

If <VAR>limit</VAR> is equal to
pi,
the points are between
10^base and pi,
<EM>not</EM>
10^base and 10^pi,
in order to  be compatible with the corresponding MATLAB function.
</P>
</DL>
<P>

<A NAME="doc-warn_neg_dim_as_zero"></A>
<A NAME="IDX600"></A>
</P>
<DL>
<DT><U>Built-in Variable:</U> <B>warn_neg_dim_as_zero</B>
<DD>If the value of <CODE>warn_neg_dim_as_zero</CODE> is nonzero, print a warning
for expressions like
<P>

<TABLE><tr><td>&nbsp;</td><td class=example><pre>eye (-1)
</pre></td></tr></table><P>

The default value is 0.
</P>
</DL>
<P>

<A NAME="doc-warn_imag_to_real"></A>
<A NAME="IDX601"></A>
</P>
<DL>
<DT><U>Built-in Variable:</U> <B>warn_imag_to_real</B>
<DD>If the value of <CODE>warn_imag_to_real</CODE> is nonzero, a warning is
printed for implicit conversions of complex numbers to real numbers.
The default value is 0.
</DL>
<P>

<A NAME="Famous Matrices"></A>
<HR SIZE="6">
<A NAME="SEC151"></A>
<TABLE CELLPADDING=1 CELLSPACING=1 BORDER=0>
<TR><TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="octave_19.html#SEC150"> &lt; </A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="octave_20.html#SEC152"> &gt; </A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT"> &nbsp; <TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="octave_19.html#SEC147"> &lt;&lt; </A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="octave_19.html#SEC147"> Up </A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="octave_20.html#SEC152"> &gt;&gt; </A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT"> &nbsp; <TD VALIGN="MIDDLE" ALIGN="LEFT"> &nbsp; <TD VALIGN="MIDDLE" ALIGN="LEFT"> &nbsp; <TD VALIGN="MIDDLE" ALIGN="LEFT"> &nbsp; <TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="octave.html#SEC_Top">Top</A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="octave_toc.html#SEC_Contents">Contents</A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="octave_42.html#SEC245">Index</A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="octave_abt.html#SEC_About"> ? </A>]</TD>
</TR></TABLE>
<H2> 18.4 Famous Matrices </H2>
<!--docid::SEC151::-->
<P>

The following functions return famous matrix forms.
</P>
<P>

<A NAME="doc-hankel"></A>
<A NAME="IDX602"></A>
</P>
<DL>
<DT><U>Function File:</U>  <B>hankel</B> <I>(<VAR>c</VAR>, <VAR>r</VAR>)</I>
<DD>Return the Hankel matrix constructed given the first column <VAR>c</VAR>, and
(optionally) the last row <VAR>r</VAR>.  If the last element of <VAR>c</VAR> is
not the same as the first element of <VAR>r</VAR>, the last element of
<VAR>c</VAR> is used.  If the second argument is omitted, it is assumed to
be a vector of zeros with the same size as <VAR>c</VAR>.
<P>

A Hankel matrix formed from an m-vector <VAR>c</VAR>, and an n-vector
<VAR>r</VAR>, has the elements
</P>
<P>

<TABLE><tr><td>&nbsp;</td><td class=example><pre>H(i,j) = c(i+j-1),  i+j-1 &lt;= m;
H(i,j) = r(i+j-m),  otherwise
</pre></td></tr></table></DL>
<P>

<A NAME="doc-hilb"></A>
<A NAME="IDX603"></A>
</P>
<DL>
<DT><U>Function File:</U>  <B>hilb</B> <I>(<VAR>n</VAR>)</I>
<DD>Return the Hilbert matrix of order <VAR>n</VAR>.  The
i, j
element of a Hilbert matrix is defined as
<P>

<TABLE><tr><td>&nbsp;</td><td class=example><pre>H (i, j) = 1 / (i + j - 1)
</pre></td></tr></table></DL>
<P>

<A NAME="doc-invhilb"></A>
<A NAME="IDX604"></A>
</P>
<DL>
<DT><U>Function File:</U>  <B>invhilb</B> <I>(<VAR>n</VAR>)</I>
<DD>Return the inverse of a Hilbert matrix of order <VAR>n</VAR>.  This can be 
computed computed exactly using
<TABLE><tr><td>&nbsp;</td><td class=example><pre>
            (i+j)         /n+i-1\  /n+j-1\   /i+j-2\ 2
 A(i,j) = -1      (i+j-1)(       )(       ) (       )
                          \ n-j /  \ n-i /   \ i-2 /

        = p(i) p(j) / (i+j-1)

</pre></td></tr></table>where
<TABLE><tr><td>&nbsp;</td><td class=example><pre>             k  /k+n-1\   /n\
    p(k) = -1  (       ) (   )
                \ k-1 /   \k/
</pre></td></tr></table><P>

The validity of this formula can easily be checked by expanding 
the binomial coefficients in both formulas as factorials.  It can 
be derived more directly via the theory of Cauchy matrices: 
see J. W. Demmel, Applied Numerical Linear Algebra, page 92.
</P>
<P>

Compare this with the numerical calculation of <CODE>inverse (hilb (n))</CODE>,
which suffers from the ill-conditioning of the Hilbert matrix, and the
finite precision of your computer's floating point arithmetic.
</P>
<P>

</P>
</DL>
<P>

<A NAME="doc-sylvester_matrix"></A>
<A NAME="IDX605"></A>
</P>
<DL>
<DT><U>Function File:</U>  <B>sylvester_matrix</B> <I>(<VAR>k</VAR>)</I>
<DD>Return the Sylvester matrix of order
n = 2^k.
</DL>
<P>

<A NAME="doc-toeplitz"></A>
<A NAME="IDX606"></A>
</P>
<DL>
<DT><U>Function File:</U>  <B>toeplitz</B> <I>(<VAR>c</VAR>, <VAR>r</VAR>)</I>
<DD>Return the Toeplitz matrix constructed given the first column <VAR>c</VAR>,
and (optionally) the first row <VAR>r</VAR>.  If the first element of <VAR>c</VAR>
is not the same as the first element of <VAR>r</VAR>, the first element of
<VAR>c</VAR> is used.  If the second argument is omitted, the first row is
taken to be the same as the first column.
<P>

A square Toeplitz matrix has the form:
</P>
<P>

<TABLE><tr><td>&nbsp;</td><td class=example><pre>c(0)  r(1)   r(2)  ...  r(n)
c(1)  c(0)   r(1)  ... r(n-1)
c(2)  c(1)   c(0)  ... r(n-2)
 .     ,      ,   .      .
 .     ,      ,     .    .
 .     ,      ,       .  .
c(n) c(n-1) c(n-2) ...  c(0)
</pre></td></tr></table></DL>
<P>

<A NAME="doc-vander"></A>
<A NAME="IDX607"></A>
</P>
<DL>
<DT><U>Function File:</U>  <B>vander</B> <I>(<VAR>c</VAR>)</I>
<DD>Return the Vandermonde matrix whose next to last column is <VAR>c</VAR>.
<P>

A Vandermonde matrix has the form:
</P>
<P>

<TABLE><tr><td>&nbsp;</td><td class=example><pre>c(1)^(n-1) ... c(1)^2  c(1)  1
c(2)^(n-1) ... c(2)^2  c(2)  1
    .     .      .      .    .
    .       .    .      .    .
    .         .  .      .    .
c(n)^(n-1) ... c(n)^2  c(n)  1
</pre></td></tr></table></DL>
<P>

<A NAME="Arithmetic"></A>
<HR SIZE="6">
<TABLE CELLPADDING=1 CELLSPACING=1 BORDER=0>
<TR><TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="octave_19.html#SEC147"> &lt;&lt; </A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="octave_20.html#SEC152"> &gt;&gt; </A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT"> &nbsp; <TD VALIGN="MIDDLE" ALIGN="LEFT"> &nbsp; <TD VALIGN="MIDDLE" ALIGN="LEFT"> &nbsp; <TD VALIGN="MIDDLE" ALIGN="LEFT"> &nbsp; <TD VALIGN="MIDDLE" ALIGN="LEFT"> &nbsp; <TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="octave.html#SEC_Top">Top</A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="octave_toc.html#SEC_Contents">Contents</A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="octave_42.html#SEC245">Index</A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="octave_abt.html#SEC_About"> ? </A>]</TD>
</TR></TABLE>
<BR>
<FONT SIZE="-1">
This document was generated
by <I>John W. Eaton</I> on <I>May, 18 2005</I>
using <A HREF="http://texi2html.cvshome.org"><I>texi2html</I></A>
</FONT>

</BODY>
</HTML>