Octave basic operation and data transfer

  • %: 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.
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 最近看了一本书 《富爸爸穷爸爸》, 作为英语专业的毕业生,我很惭愧地说这是我看的第一本完整英文原著。一直以来脑子里...
    水心沁阅读 4,569评论 0 2
  • 我小的时候爷爷奶奶家里养有鸟,一黄一绿伶俐可爱,爷爷每天给它们投食打扫,奶奶看着也甚是喜欢,只听说后来鸟儿飞走了,...
    晴天阿阅读 1,014评论 0 0
  • 一直很喜欢王家卫的重庆森林,第一遍看的确没看明白,第二遍再看有些理解,后来反复的看再加上自己的经历便得出了结论,不...
    唯小曼阅读 1,768评论 2 2
  • 1 在目前,老师讲、学生听是这个时代大概率的教育模式。我们吐槽应试教育、填鸭式教学,这种被动式学习如果没有得到践行...
    琳娜911阅读 1,183评论 0 2