Octave教程三:Computing On Data

代码如下:

>> A = [1 2;3 4;5 6]
A =

   1   2
   3   4
   5   6

>> B = [11 12;13 14;15 16]
B =

   11   12
   13   14
   15   16

>> C = [1 1;2 2]
C =

   1   1
   2   2

>> A * C
ans =

    5    5
   11   11
   17   17

>> A .* B
ans =

   11   24
   39   56
   75   96

>> A
A =

   1   2
   3   4
   5   6

>> A .^ 2
ans =

    1    4
    9   16
   25   36

>> V = [1;2;3]
V =

   1
   2
   3

>> 1 ./ V
ans =

   1.00000
   0.50000
   0.33333

>> 1 ./a
error: 'a' undefined near line 1 column 5
>> 1 ./ A
ans =

   1.00000   0.50000
   0.33333   0.25000
   0.20000   0.16667

>> log(V)
ans =

   0.00000
   0.69315
   1.09861

>> exp(V)
ans =

    2.7183
    7.3891
   20.0855

>> V
V =

   1
   2
   3

>> abs(V)
ans =

   1
   2
   3

>> V = [1 -2 3]
V =

   1  -2   3

>> V = [1;-2;3]
V =

   1
  -2
   3

>> abs(V)
ans =

   1
   2
   3

>> -V
ans =

  -1
   2
  -3

>> V + ones(length(V),1)
ans =

   2
  -1
   4

>> V
V =

   1
  -2
   3

>> V + 1
ans =

   2
  -1
   4

>> V
V =

   1
  -2
   3

>> A
A =

   1   2
   3   4
   5   6

>> A'
ans =

   1   3   5
   2   4   6

>> (A')'
ans =

   1   2
   3   4
   5   6

>> a = [1 15 2 0.5]
a =

    1.00000   15.00000    2.00000    0.50000

>> val = max(a)
val =  15
>> [val ind] = max(a)
val =  15
ind =  2
>> [anhaibo hushiyun] = max(a)
anhaibo =  15
hushiyun =  2
>> A
A =

   1   2
   3   4
   5   6

>> max(A)
ans =

   5   6

>> A = [1 2;3 4;4 3]
A =

   1   2
   3   4
   4   3

>> max(A)
ans =

   4   4

>> A = [5 4;2 1;4 6]
A =

   5   4
   2   1
   4   6

>> max(A)
ans =

   5   6

>> a
a =

    1.00000   15.00000    2.00000    0.50000

>> find(a < 3)
ans =

   1   3   4

>> find (a > 1)
ans =

   2   3

>> A = magic(3)
A =

   8   1   6
   3   5   7
   4   9   2

>> help magic
'magic' is a function from the file D:\soft\Octave\OCTAVE~1.1\share\octave\4.2.1\m\special-matrix\magic.m

 -- magic (N)

     Create an N-by-N magic square.

     A magic square is an arrangement of the integers '1:n^2' such that
     the row sums, column sums, and diagonal sums are all equal to the
     same value.

     Note: N must be a scalar greater than or equal to 3.  If you supply
     N less than 3, magic returns either a nonmagic square, or else the
     degenerate magic squares 1 and [].

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 http://www.octave.org and via the help@octave.org
mailing list.
>> [r,c] = find(A >= 7)
r =

   1
   3
   2

c =

   1
   2
   3

>> a
a =

    1.00000   15.00000    2.00000    0.50000

>> sum(a)
ans =  18.500
>> a = [1.2 2.3 4.5 6.7]
a =

   1.2000   2.3000   4.5000   6.7000

>> prod(a)
ans =  83.214
>> help prod
'prod' is a built-in function from the file libinterp/corefcn/data.cc

 -- prod (X)
 -- prod (X, DIM)
 -- prod (..., "native")
 -- prod (..., "double")
     Product of elements along dimension DIM.

     If DIM is omitted, it defaults to the first non-singleton
     dimension.

     The optional "type" input determines the class of the variable used
     for calculations.  If the argument "native" is given, then the
     operation is performed in the same type as the original argument,
     rather than the default double type.

     For example:

          prod ([true, true])
             => 1
          prod ([true, true], "native")
             => true

     On the contrary, if "double" is given, the operation is performed
     in double precision even for single precision inputs.

     See also: cumprod, sum.

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
>> floor(a)
ans =

   1   2   4   6

>> a
a =

   1.2000   2.3000   4.5000   6.7000

>> ceil(a)
ans =

   2   3   5   7

>> a
a =

   1.2000   2.3000   4.5000   6.7000

>> rand(3)
ans =

   0.233792   0.822080   0.582898
   0.100933   0.958796   0.777936
   0.080369   0.399305   0.445063

>> rand (3)
ans =

   0.297022   0.026104   0.050976
   0.557869   0.813663   0.404590
   0.420324   0.596950   0.858608

>> ONE = [1 3 5;6 4 7;8 3 5]
ONE =

   1   3   5
   6   4   7
   8   3   5

>> TWO = [2 4 4;5 5 8;7 2 6]
TWO =

   2   4   4
   5   5   8
   7   2   6

>> max(A,B)
error: max: nonconformant arguments (op1 is 3x3, op2 is 3x2)
>> #max(ONE,TWO
>> max(ONE,TWO)
ans =

   2   4   5
   6   5   8
   8   3   6

>> A
A =

   8   1   6
   3   5   7
   4   9   2

>> amx(A,[],1)
error: 'amx' undefined near line 1 column 1
>> max(A,[],1)
ans =

   8   9   7

>> max(A,[],2)
ans =

   8
   7
   9

>> max(A)
ans =

   8   9   7

>> max(max(A))
ans =  9
>> A(:)
ans =

   8
   3
   4
   1
   5
   9
   6
   7
   2

>> A
A =

   8   1   6
   3   5   7
   4   9   2

>> max(A(:))
ans =  9
>> A = magic(9)
A =

   47   58   69   80    1   12   23   34   45
   57   68   79    9   11   22   33   44   46
   67   78    8   10   21   32   43   54   56
   77    7   18   20   31   42   53   55   66
    6   17   19   30   41   52   63   65   76
   16   27   29   40   51   62   64   75    5
   26   28   39   50   61   72   74    4   15
   36   38   49   60   71   73    3   14   25
   37   48   59   70   81    2   13   24   35

>> sum(A,1)
ans =

   369   369   369   369   369   369   369   369   369

>> sum(A,2)
ans =

   369
   369
   369
   369
   369
   369
   369
   369
   369

>> A .* eye(9)
ans =

   47    0    0    0    0    0    0    0    0
    0   68    0    0    0    0    0    0    0
    0    0    8    0    0    0    0    0    0
    0    0    0   20    0    0    0    0    0
    0    0    0    0   41    0    0    0    0
    0    0    0    0    0   62    0    0    0
    0    0    0    0    0    0   74    0    0
    0    0    0    0    0    0    0   14    0
    0    0    0    0    0    0    0    0   35

>> sum(sum(A.*eye(9)))
ans =  369
>> sum(sum(A.*flipud(eye(9))))
ans =  369
>> eye(9)
ans =

Diagonal Matrix

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

>> flipud(eye(9))
ans =

Permutation Matrix

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

>> A = magic(3)
A =

   8   1   6
   3   5   7
   4   9   2

>> pinv(A)
ans =

   0.147222  -0.144444   0.063889
  -0.061111   0.022222   0.105556
  -0.019444   0.188889  -0.102778

>> B = pinv(A)
B =

   0.147222  -0.144444   0.063889
  -0.061111   0.022222   0.105556
  -0.019444   0.188889  -0.102778

>> A * B
ans =

  1.0000e+000  -1.2157e-014  6.3560e-015
  5.5511e-017  1.0000e+000  -1.5266e-016
  -5.9813e-015  1.2268e-014  1.0000e+000

>>
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值