Simple Octave Operatons


malt> octave
GNU Octave, version 2.1.33 (i386-redhat-linux-gnu).
Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001 John W. Eaton.
This is free software with ABSOLUTELY NO WARRANTY.
For details, type `warranty'.

*** This is a development version of Octave.  Development releases
*** are provided for people who want to help test, debug, and improve
*** Octave.
***
*** If you want a stable, well-tested version of Octave, you should be
*** using one of the stable releases (when this development release
*** was made, the latest stable version was 2.0.16).

octave:1> # A simple example of using Octave
octave:1> # Turn on the diary
octave:1> diary on
octave:2>
octave:2> # Output the value of a built-in variable
octave:2> DEFAULT_LOADPATH
DEFAULT_LOADPATH = .:/usr/libexec/octave/2.1.33/site/oct/i386-redhat-linux-gnu//:/usr/libexec/octave/site/oct/i386-redhat-linux-gnu//:/usr/share/octave/2.1.33/site/m//:/usr/share/octave/site/m//:/usr/libexec/octave/2.1.33/oct/i386-redhat-linux-gnu//:/usr/share/octave/2.1.33/m//
octave:3>
octave:3>
octave:3> # Some simple arithmetic
octave:3>
octave:3> x = 10
x = 10
octave:4> y = 4
y = 4
octave:5> x + y
ans = 14
octave:6> z = (x + y) / 2
z = 7
octave:7> disp (z)
7
octave:8> printf ("%d = %d + %d\n", z, x, y);
7 = 10 + 4
octave:9>
octave:9>
octave:9>
octave:9> # Build a row vector
octave:9>
octave:9> rv = [2, 4, 6, 7]
rv =

  2  4  6  7

octave:10> # Sum all the elements
octave:10>
octave:10> sum (rv)
ans = 19
octave:11> # Transpose
octave:11> rv'
ans =

  2
  4
  6
  7

octave:12> # Product of elements
octave:12> prod (rv)
ans = 336
octave:13> # Obvious
octave:13> sort (rv)
ans =

  2  4  6  7

octave:14> #Add elements
octave:14>
octave:14> rv = [rv 0 5 8 2 0]
rv =

  2  4  6  7  0  5  8  2  0

octave:15> sort (rv)
ans =

  0  0  2  2  4  5  6  7  8

octave:16> # find nonzero elements
octave:16> find (rv)
ans =

  1  2  3  4  6  7  8

octave:17> rv ([find(rv)])
ans =

  2  4  6  7  5  8  2

octave:18>
octave:18> # Create a matrix
octave:18>
octave:18> #Create a column vector
octave:18>
octave:18> cv = [5;2;0;6;9]
cv =

  5
  2
  0
  6
  9

octave:19> # Simple arithmetic
octave:19> cv + 2
ans =

   7
   4
   2
   8
  11

octave:20> cv * 2
ans =

  10
   4
   0
  12
  18

octave:21> x ^ 2
ans = 100
octave:22> # Power function
octave:22> cv ^ 2
error: for A^b, A must be square
error: evaluating binary operator `^' near line 22, column 4
octave:22> cv .^ 2
ans =

  25
   4
   0
  36
  81

octave:23> x < 4
ans = 0
octave:24> cv < 4
ans =

  0
  1
  1
  0
  0

octave:25>
octave:25>
octave:25> # Create a matrix
octave:25>
octave:25> m = [1 3 5;2 4 6]
m =

  1  3  5
  2  4  6

octave:26> # Transpose
octave:26> m'
ans =

  1  2
  3  4
  5  6

octave:27> m.'
ans =

  1  2
  3  4
  5  6

octave:28> m * 2
ans =

   2   6  10
   4   8  12

octave:29> m ./ 2
ans =

  0.50000  1.50000  2.50000
  1.00000  2.00000  3.00000

octave:30> m .\ 2
ans =

  2.00000  0.66667  0.40000
  1.00000  0.50000  0.33333

octave:31> # Add with matrix
octave:31> m + [2 4 2]
error: operator +: nonconformant arguments (op1 is 2x3, op2 is 1x3)
error: evaluating binary operator `+' near line 31, column 3

octave:31> m + [1;2]
error: operator +: nonconformant arguments (op1 is 2x3, op2 is 2x1)
error: evaluating binary operator `+' near line 31, column 3
octave:31> m2 = [2 2 4;1 3 1]
m2 =

  2  2  4
  1  3  1

octave:32> # Matrix multiplication
octave:32> m * m2'
ans =

  28  15
  36  20

octave:33> # Element by element multiplication
octave:33> m .* m2
ans =

   2   6  20
   2  12   6

octave:35> # Applying functions to the matrix
octave:35> max (m)
ans =

  2  4  6

octave:36> max (m')
ans =

  5  6

octave:37> max (max (m))
ans = 6

octave:38> reshape (m, 6,1)
ans =

  1
  2
  3
  4
  5
  6

octave:39> m
m =

  1  3  5
  2  4  6

octave:40> prod (m)
ans =

   2  12  30

octave:41> rem (m, m2)
ans =

  1  1  1
  0  1  0

octave:42> rem (m, 4)
ans =

  1  3  1
  2  0  2

octave:43> s = [5 2 1; 6 6 2; 3 3 4]
s =

  5  2  1
  6  6  2
  3  3  4

octave:44> diag (s)
ans =

  5
  6
  4

octave:45> inv (s)
ans =

   0.33333  -0.09259  -0.03704
  -0.33333   0.31481  -0.07407
   0.00000  -0.16667   0.33333

octave:46> pinv (s)
ans =

    3.3333e-01   -9.2593e-02   -3.7037e-02
   -3.3333e-01    3.1481e-01   -7.4074e-02
   -4.1660e-17   -1.6667e-01    3.3333e-01

octave:47> eig (s)
ans =

  10.4051
   2.0000
   2.5949

octave:48> det (s)
ans = 54.000
octave:49> s * inv (s)
ans =

   1.00000  -0.00000   0.00000
  -0.00000   1.00000   0.00000
  -0.00000   0.00000   1.00000

octave:50> rows (s)
ans = 3
octave:51> columns (s)
ans = 3
octave:52> s > 4
ans =

  1  0  0
  1  1  0
  0  0  0

octave:53> # Special matrices
octave:53>
octave:53> eye (3,4)
ans =

  1  0  0  0
  0  1  0  0
  0  0  1  0

octave:54> ones (2,2)
ans =

  1  1
  1  1

octave:55> zeros (5,1)
ans =

  0
  0
  0
  0
  0

octave:56> rand (3,3)
ans =

  0.021344  0.876873  0.080671
  0.123355  0.996146  0.932937
  0.892503  0.413795  0.701691

octave:57> sum(rem(s,s')==0)==2
ans =

  0  0  0

octave:58> s (2,3)
ans = 2
octave:59> s (2,3) = 8
s =

  5  2  1
  6  6  8
  3  3  4
 
octave:60> s(1:2,2)
ans =
 
  2
  6
 
octave:61> s(1:2,2:3)
ans =
 
  2  1
  6  8
 
octave:62> s(1)
error: single index only valid for row or column vector
octave:62> s(1,:)
ans =
 
  5  2  1
 
octave:63> disp (pi)
3.1416
octave:64> diary off
octave:65>