美文网首页
Octave basic operation and data

Octave basic operation and data

作者: LinKuriboh | 来源:发表于2019-03-24 22:00 被阅读0次
  • %: stand for comment

  • ~=: stand for unequal

  • Semicolon will suppress output

  • disp:

octave:6> a = pi;
octave:7> disp(sprintf('2 decimal: %0.2f', a))
2 decimal: 3.14
  • format long && format short
octave:8> format long
octave:9> a
a =  3.141592653589793
octave:10> format short
octave:11> a
a =  3.1416
octave:12> 
  • Generate a matrix or a vector
octave:12> matrix_a = [1 2; 3 4; 5 6];
octave:13> matrix_a
matrix_a =

   1   2
   3   4
   5   6

octave:14> vector_a = [1 2 3]
vector_a =

   1   2   3

octave:15> vector_b = 1:0.1:2
vector_b =

 Columns 1 through 8:

    1.0000    1.1000    1.2000    1.3000    1.4000    1.5000    1.6000    1.7000

 Columns 9 through 11:

    1.8000    1.9000    2.0000

octave:14> vector_a = [1 2 3]
vector_a =

   1   2   3

octave:15> vector_b = 1:0.1:2
vector_b =

 Columns 1 through 8:

    1.0000    1.1000    1.2000    1.3000    1.4000    1.5000    1.6000    1.7000

 Columns 9 through 11:

    1.8000    1.9000    2.0000
octave:16> vector_c = 1:6
vector_c =

   1   2   3   4   5   6
  • Get a part of matrix:
octave:21> matrix_a(3, 2)
ans =  6
octave:22> matrix_a(3, :)
ans =

   5   6

octave:23> matrix_a(:, 2)
ans =

   2
   4
   6

octave:24> matrix_a([1, 3], :)
ans =

   1   2
   5   6


  • Reassign the value in matrix
octave:25> matrix_a(:, 2) = [10, 11, 12]
matrix_a =

    1   10
    3   11
    5   12


  • Append a vector in matrix:
octave:27> [matrix_a, [20; 21; 22]]
ans =

    1   10   20
    3   11   21
    5   12   22
  • Put all element together in a column vector
octave:28> matrix_a(:)
ans =

    1
    3
    5
   10
   11
   12
  • Concatenate matrix
octave:29> matrix_a = [1 2; 3 4; 5 6]
matrix_a =

   1   2
   3   4
   5   6

octave:30> matrix_b = [11 12; 13 14; 15 16]
matrix_b =

   11   12
   13   14
   15   16

octave:31> matrix_c = [matrix_a matrix_b]
matrix_c =

    1    2   11   12
    3    4   13   14
    5    6   15   16

octave:32> matrix_d = [matrix_a; matrix_b]
matrix_d =

    1    2
    3    4
    5    6
   11   12
   13   14
   15   16


  • Print a histogram
octave:1> w = -6 + sqrt(10)*(randn(1, 10000));
octave:2> hist(w)
octave:3> 
Screen Shot 2019-03-23 at 23.27.18.png
  • Generate an identity matrix
octave:1> eye(5)
ans =

Diagonal Matrix

   1   0   0   0   0
   0   1   0   0   0
   0   0   1   0   0
   0   0   0   1   0
   0   0   0   0   1
  • size
octave:4> size(matrix_a)
ans =

   3   2

octave:5> size(matrix_a, 1)
ans =  3
octave:6> size(matrix_a, 2)
ans =  2
  • length: return the longest dimension of matrix
octave:7> length(matrix_a)
ans =  3
  • who && whos: List all variables in current scope
octave:9> who
Variables in the current scope:

ans       matrix_a

octave:10> a = 1
a =  1
octave:11> who
Variables in the current scope:

a         ans       matrix_a

octave:12> whos
Variables in the current scope:

   Attr Name          Size                     Bytes  Class
   ==== ====          ====                     =====  ===== 
        a             1x1                          8  double
        ans           1x15                        15  char
        matrix_a      3x2                         48  double
  • pwd: show the current location
octave:8> pwd
ans = /Users/xxx
  • load: to import data files
octave:25> load('ex1data1.txt')
octave:26> whos
Variables in the current scope:

   Attr Name          Size                     Bytes  Class
   ==== ====          ====                     =====  ===== 
        ans           1x25                        25  char
        ex1data1      97x2                      1552  double
        h0            1x101                      808  double
        y             1x101                      808  double
        y2            1x101                      808  double
  • clear : to delete a variable
octave:15> clear a
octave:16> whos
Variables in the current scope:

   Attr Name          Size                     Bytes  Class
   ==== ====          ====                     =====  ===== 
        ans           1x25                        25  char
        matrix_a      3x2                         48  double

Total is 31 elements using 73 bytes
  • help

see document

octave:2> help eye
'eye' is a built-in function from the file libinterp/corefcn/data.cc

 -- eye (N)
 -- eye (M, N)
 -- eye ([M N])
 -- eye (..., CLASS)
     Return an identity matrix.

     If invoked with a single scalar argument N, return a square NxN
     identity matrix.

     If supplied two scalar arguments (M, N), 'eye' takes them to be the
     number of rows and columns.  If given a vector with two elements,
     'eye' uses the values of the elements as the number of rows and
     columns, respectively.  For example:

          eye (3)
           =>  1  0  0
               0  1  0
               0  0  1

     The following expressions all produce the same result:

          eye (2)
          ==
          eye (2, 2)
          ==
          eye (size ([1, 2; 3, 4]))

     The optional argument CLASS, allows 'eye' to return an array of the
     specified type, like

          val = zeros (n,m, "uint8")

     Calling 'eye' with no arguments is equivalent to calling it with an
     argument of 1.  Any negative dimensions are treated as zero.  These
     odd definitions are for compatibility with MATLAB.

     See also: speye, ones, zeros.

Additional help for built-in functions and operators is
available in the online version of the manual.  Use the command
'doc <topic>' to search the manual index.

Help and information about Octave is also available on the WWW
at https://www.octave.org and via the help@octave.org
mailing list.

相关文章

网友评论

      本文标题:Octave basic operation and data

      本文链接:https://www.haomeiwen.com/subject/bhnyvqtx.html