File: octave_16.html

package info (click to toggle)
octave 2.0.16-2
  • links: PTS
  • area: main
  • in suites: potato
  • size: 26,276 kB
  • ctags: 16,450
  • sloc: cpp: 67,548; fortran: 41,514; ansic: 26,682; sh: 7,361; makefile: 4,077; lex: 2,008; yacc: 1,849; lisp: 1,702; perl: 1,676; exp: 123
file content (907 lines) | stat: -rw-r--r-- 21,158 bytes parent folder | download
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
<HTML>
<HEAD>
<!-- This HTML file has been created by texi2html 1.51
     from ./octave.texi on 18 June 1999 -->

<TITLE>GNU Octave - Matrix Manipulation</TITLE>
</HEAD>
<BODY>
Go to the <A HREF="octave_1.html">first</A>, <A HREF="octave_15.html">previous</A>, <A HREF="octave_17.html">next</A>, <A HREF="octave_40.html">last</A> section, <A HREF="octave_toc.html">table of contents</A>.
<P><HR><P>


<H1><A NAME="SEC131" HREF="octave_toc.html#TOC131">Matrix Manipulation</A></H1>

<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>



<H2><A NAME="SEC132" HREF="octave_toc.html#TOC132">Finding Elements and Checking Conditions</A></H2>

<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>
<DL>
<DT><U>Built-in Function:</U>  <B>any</B> <I>(<VAR>x</VAR>)</I>
<DD><A NAME="IDX574"></A>
For a vector argument, return 1 if any element of the vector is
nonzero.

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

<PRE>
any (eye (2, 4))
     => [ 1, 1, 0, 0 ]
</PRE>

<P>
To see if any of the elements of a matrix are nonzero, you can use a
statement like

</P>

<PRE>
any (any (a))
</PRE>

</DL>

<P>
<DL>
<DT><U>Built-in Function:</U>  <B>all</B> <I>(<VAR>x</VAR>)</I>
<DD><A NAME="IDX575"></A>
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 in a column of a matrix, are nonzero.
</DL>

</P>
<P>
Since the comparison operators (see section <A HREF="octave_9.html#SEC70">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>

<PRE>
all (all (rand (5) &#60; 0.9))
     => 0
</PRE>

<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>
<DL>
<DT><U>Function File:</U> [<VAR>err</VAR>, <VAR>y1</VAR>, ...] = <B>common_size</B> <I>(<VAR>x1</VAR>, ...)</I>
<DD><A NAME="IDX576"></A>
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>

<PRE>
[errorcode, a, b] = common_size ([1 2; 3 4], 5)
     => errorcode = 0
     => a = [ 1, 2; 3, 4 ]
     => b = [ 5, 5; 5, 5 ]
</PRE>

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

</P>
<P>
<DL>
<DT><U>Function File:</U>  <B>diff</B> <I>(<VAR>x</VAR>, <VAR>k</VAR>)</I>
<DD><A NAME="IDX577"></A>
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), ..., <VAR>x</VAR>(n) - <VAR>x</VAR>(n-1).

</P>
<P>
If <VAR>x</VAR> is a matrix, <CODE>diff (<VAR>x</VAR>)</CODE> is the matrix of column
differences.

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

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

</P>

<PRE>
isinf ([13, Inf, NaN])
     => [ 0, 1, 0 ]
</PRE>

</DL>

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

</P>

<PRE>
isnan ([13, Inf, NaN])
     => [ 0, 0, 1 ]
</PRE>

</DL>

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

</P>

<PRE>
finite ([13, Inf, NaN])
     => [ 1, 0, 0 ]
</PRE>

</DL>

<P>
<DL>
<DT><U>Loadable Function:</U>  <B>find</B> <I>(<VAR>x</VAR>)</I>
<DD><A NAME="IDX581"></A>
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>

<PRE>
find (eye (2))
     => [ 1; 4 ]
</PRE>

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

</P>

<PRE>
[i, j] = find (2 * eye (2))
     => i = [ 1; 2 ]
     => j = [ 1; 2 ]
</PRE>

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

</P>

<PRE>
[i, j, v] = find (3 * eye (2))
     => i = [ 1; 2 ]
     => j = [ 1; 2 ]
     => v = [ 3; 3 ]
</PRE>

</DL>
<P>
        


<H2><A NAME="SEC133" HREF="octave_toc.html#TOC133">Rearranging Matrices</A></H2>

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

</P>

<PRE>
fliplr ([1, 2; 3, 4])
     =>  2  1
         4  3
</PRE>

</DL>

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

</P>

<PRE>
flipud ([1, 2; 3, 4])
     =>  3  4
         1  2
</PRE>

</DL>

<P>
<DL>
<DT><U>Function File:</U>  <B>rot90</B> <I>(<VAR>x</VAR>, <VAR>n</VAR>)</I>
<DD><A NAME="IDX584"></A>
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>

<PRE>
rot90 ([1, 2; 3, 4], -1)
     =>  3  1
         4  2
</PRE>

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

</P>

<PRE>
rot90 ([1, 2; 3, 4], -1)
==
rot90 ([1, 2; 3, 4], 3)
==
rot90 ([1, 2; 3, 4], 7)
</PRE>

</DL>

<P>
<DL>
<DT><U>Function File:</U>  <B>reshape</B> <I>(<VAR>a</VAR>, <VAR>m</VAR>, <VAR>n</VAR>)</I>
<DD><A NAME="IDX585"></A>
Return a matrix with <VAR>m</VAR> rows and <VAR>n</VAR> columns whose elements are
taken from the matrix <VAR>a</VAR>.  To decide how to order the elements,
Octave pretends that the elements of a matrix are stored in column-major
order (like Fortran arrays are stored).

</P>
<P>
For example,

</P>

<PRE>
reshape ([1, 2, 3, 4], 2, 2)
     =>  1  3
         2  4
</PRE>

<P>
If the variable <CODE>do_fortran_indexing</CODE> is nonzero, the
<CODE>reshape</CODE> function is equivalent to

</P>

<PRE>
retval = zeros (m, n);
retval (:) = a;
</PRE>

<P>
but it is somewhat less cryptic to use <CODE>reshape</CODE> instead of the
colon operator.  Note that the total number of elements in the original
matrix must match the total number of elements in the new matrix.
</DL>

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

</P>
<P>
If <VAR>x</VAR> is a matrix, do the same for each column of <VAR>x</VAR>.
</DL>

</P>
<P>
<DL>
<DT><U>Loadable Function:</U> [<VAR>s</VAR>, <VAR>i</VAR>] = <B>sort</B> <I>(<VAR>x</VAR>)</I>
<DD><A NAME="IDX587"></A>
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>
<P>
For example,

</P>

<PRE>
sort ([1, 2; 2, 3; 3, 1])
     =>  1  1
         2  2
         3  3
</PRE>

<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>

<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>

</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="DOCF6" HREF="octave_foot.html#FOOT6">(6)</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>

<PRE>
a = [1, 2; 2, 3; 3, 1];
[s, i] = sort (a (:, 2));
a (i, :)
     =>  3  1
         1  2
         2  3
</PRE>

<P>
<DL>
<DT><U>Function File:</U>  <B>tril</B> <I>(<VAR>a</VAR>, <VAR>k</VAR>)</I>
<DD><A NAME="IDX588"></A>
<DT><U>Function File:</U>  <B>triu</B> <I>(<VAR>a</VAR>, <VAR>k</VAR>)</I>
<DD><A NAME="IDX589"></A>
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>
<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>

<PRE>
tril (ones (3), -1)
     =>  0  0  0
         1  0  0
         1  1  0
</PRE>

<P>
and

</P>

<PRE>
tril (ones (3), 1)
     =>  1  1  0
         1  1  1
         1  1  1
</PRE>

</DL>

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

</P>
<P>
<DL>
<DT><U>Function File:</U>  <B>vech</B> <I>(<VAR>x</VAR>)</I>
<DD><A NAME="IDX591"></A>
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>


<H2><A NAME="SEC134" HREF="octave_toc.html#TOC134">Special Utility Matrices</A></H2>

<P>
<DL>
<DT><U>Built-in Function:</U>  <B>eye</B> <I>(<VAR>x</VAR>)</I>
<DD><A NAME="IDX592"></A>
<DT><U>Built-in Function:</U>  <B>eye</B> <I>(<VAR>n</VAR>, <VAR>m</VAR>)</I>
<DD><A NAME="IDX593"></A>
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>

<PRE>
eye (3)
     =>  1  0  0
         0  1  0
         0  0  1
</PRE>

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

</P>

<PRE>
eye (2)
==
eye (2, 2)
==
eye (size ([1, 2; 3, 4])
</PRE>

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

</P>
<P>
<DL>
<DT><U>Built-in Function:</U>  <B>ones</B> <I>(<VAR>x</VAR>)</I>
<DD><A NAME="IDX594"></A>
<DT><U>Built-in Function:</U>  <B>ones</B> <I>(<VAR>n</VAR>, <VAR>m</VAR>)</I>
<DD><A NAME="IDX595"></A>
Return a matrix whose elements are all 1.  The arguments are handled
the same as the arguments for <CODE>eye</CODE>.

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

</P>

<PRE>
val_matrix = val * ones (n, m)
</PRE>

</DL>

<P>
<DL>
<DT><U>Built-in Function:</U>  <B>zeros</B> <I>(<VAR>x</VAR>)</I>
<DD><A NAME="IDX596"></A>
<DT><U>Built-in Function:</U>  <B>zeros</B> <I>(<VAR>n</VAR>, <VAR>m</VAR>)</I>
<DD><A NAME="IDX597"></A>
Return a matrix whose elements are all 0.  The arguments are handled
the same as the arguments for <CODE>eye</CODE>.
</DL>

</P>
<P>
<DL>
<DT><U>Loadable Function:</U>  <B>rand</B> <I>(<VAR>x</VAR>)</I>
<DD><A NAME="IDX598"></A>
<DT><U>Loadable Function:</U>  <B>rand</B> <I>(<VAR>n</VAR>, <VAR>m</VAR>)</I>
<DD><A NAME="IDX599"></A>
<DT><U>Loadable Function:</U>  <B>rand</B> <I>(<CODE>"seed"</CODE>, <VAR>x</VAR>)</I>
<DD><A NAME="IDX600"></A>
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>

<PRE>
rand ("seed", <VAR>x</VAR>)
</PRE>

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

</P>

<PRE>
rand ("seed")
</PRE>

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

</P>
<P>
<DL>
<DT><U>Loadable Function:</U>  <B>randn</B> <I>(<VAR>x</VAR>)</I>
<DD><A NAME="IDX601"></A>
<DT><U>Loadable Function:</U>  <B>randn</B> <I>(<VAR>n</VAR>, <VAR>m</VAR>)</I>
<DD><A NAME="IDX602"></A>
<DT><U>Loadable Function:</U>  <B>randn</B> <I>(<CODE>"seed"</CODE>, <VAR>x</VAR>)</I>
<DD><A NAME="IDX603"></A>
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>

<PRE>
randn ("seed", <VAR>x</VAR>)
</PRE>

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

</P>

<PRE>
randn ("seed")
</PRE>

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

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

</P>

<PRE>
rand ("seed", 13);
randn ("seed", 13);
u = rand (100, 1);
n = randn (100, 1);
</PRE>

<P>
and

</P>

<PRE>
rand ("seed", 13);
randn ("seed", 13);
u = zeros (100, 1);
n = zeros (100, 1);
for i = 1:100
  u(i) = rand ();
  n(i) = randn ();
end
</PRE>

<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>
<DL>
<DT><U>Built-in Function:</U>  <B>diag</B> <I>(<VAR>v</VAR>, <VAR>k</VAR>)</I>
<DD><A NAME="IDX604"></A>
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>

<PRE>
diag ([1, 2, 3], 1)
     =>  0  1  0  0
         0  0  2  0
         0  0  0  3
         0  0  0  0
</PRE>

</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#SEC51">Ranges</A>.

</P>
<P>
<DL>
<DT><U>Function File:</U>  <B>linspace</B> <I>(<VAR>base</VAR>, <VAR>limit</VAR>, <VAR>n</VAR>)</I>
<DD><A NAME="IDX605"></A>
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>
<P>
The <CODE>linspace</CODE> function always returns a row vector, regardless of
the value of <CODE>prefer_column_vectors</CODE>.
</DL>

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

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

</P>
<P>
<DL>
<DT><U>Built-in Variable:</U> <B>treat_neg_dim_as_zero</B>
<DD><A NAME="IDX607"></A>
If the value of <CODE>treat_neg_dim_as_zero</CODE> is nonzero, expressions
like

</P>

<PRE>
eye (-1)
</PRE>

<P>
produce an empty matrix (i.e., row and column dimensions are zero).
Otherwise, an error message is printed and control is returned to the
top level.  The default value is 0.
</DL>

</P>


<H2><A NAME="SEC135" HREF="octave_toc.html#TOC135">Famous Matrices</A></H2>

<P>
The following functions return famous matrix forms.

</P>
<P>
<DL>
<DT><U>Function File:</U>  <B>hankel</B> <I>(<VAR>c</VAR>, <VAR>r</VAR>)</I>
<DD><A NAME="IDX608"></A>
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, the last row is
taken to be the same as the first column.

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

</P>

<PRE>
H (i, j) = c (i+j-1),  i+j-1 &#60;= m;
H (i, j) = r (i+j-m),  otherwise
</PRE>

</DL>

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

</P>

<PRE>
H (i, j) = 1 / (i + j - 1)
</PRE>

</DL>

<P>
<DL>
<DT><U>Function File:</U>  <B>invhilb</B> <I>(<VAR>n</VAR>)</I>
<DD><A NAME="IDX610"></A>
Return the inverse of a Hilbert matrix of order <VAR>n</VAR>.  This is exact.
Compare 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.
</DL>

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

</P>
<P>
<DL>
<DT><U>Function File:</U>  <B>toeplitz</B> <I>(<VAR>c</VAR>, <VAR>r</VAR>)</I>
<DD><A NAME="IDX612"></A>
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>
<P>
A square Toeplitz matrix has the form

</P>

<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>

</DL>

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

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

</P>

<PRE>
c(0)^n ... c(0)^2  c(0)  1
c(1)^n ... c(1)^2  c(1)  1
 .           .      .    .
 .           .      .    .
 .           .      .    .
                 
c(n)^n ... c(n)^2  c(n)  1
</PRE>

</DL>

<P><HR><P>
Go to the <A HREF="octave_1.html">first</A>, <A HREF="octave_15.html">previous</A>, <A HREF="octave_17.html">next</A>, <A HREF="octave_40.html">last</A> section, <A HREF="octave_toc.html">table of contents</A>.
</BODY>
</HTML>