美文网首页
2019-02-13 Octave计算数据

2019-02-13 Octave计算数据

作者: 孟媛的笔记 | 来源:发表于2019-02-14 16:15 被阅读0次
    >> 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 .^ 2    %A中的每个元素都平方
    ans =
    
        1    4
        9   16
       25   36
    
    

     

    >> v = [1; 2; 3]
    v =
    
       1
       2
       3
    
    >> 1 ./ v   %对每一个元素求倒数
    ans =
    
       1.00000
       0.50000
       0.33333
    
    >> log(v)    %对每一个元素求对数
    ans =
    
       0.00000
       0.69315
       1.09861
    
    >> exp(v)    %以每个元素为指数,求e的幂
    ans =
    
        2.7183
        7.3891
       20.0855
    
    >> abs(v)    %求每个元素的绝对值
    ans =
    
       1
       2
       3
    
    >> abs([-1; 2; -3])
    ans =
    
       1
       2
       3
    
    

     

    >> -v    % -1*v
    ans =
    
      -1
      -2
      -3
    
    >> v + ones(size(v))   %给每个元素加1
    ans =
    
       2
       3
       4
    
    >> ones(size(v))
    ans =
    
       1
       1
       1
    
    >> v + 1
    ans =
    
       2
       3
       4
    
    >> A
    A =
    
       1   2
       3   4
       5   6
    
    >> A'    %transpose
    ans =
    
       1   3   5
       2   4   6
    
    >> (A')'
    ans =
    
       1   2
       3   4
       5   6
    
    

     

    >> a
    a =
    
        1.00000   15.00000    2.00000    0.50000
    
    >> val = max(a)
    val =  15
    >> [val, ind] = max(a)    %ind是最大值val的索引
    val =  15
    ind =  2
    >>
    >>
    >> A
    A =
    
       1   2
       3   4
       5   6
    
    >> max(A)   %求每一列的最大值
    ans =
    
       5   6
    
    

     

    >> a
    a =
    
        1.00000   15.00000    2.00000    0.50000
    
    >> a < 3    %逐个对比
    ans =
    
      1  0  1  1
    
    >> find(a < 3)    %满足条件的索引
    ans =
    
       1   3   4
    
    >>
    >>
    >> A = magic(3)    %生成3X3幻方,每行每列每条对角线之和均相等
    A =
    
       8   1   6
       3   5   7
       4   9   2
    
    >> [r, c] = find(A >= 7)    %r是row   c是column
    r =
    
       1
       3
       2
    
    c =
    
       1
       2
       3
    
    >> A(r(1), c(1))
    ans =  8
    >> A(r(2), c(2))
    ans =  9
    >> A(r(3), c(3))
    ans =  7
    >>
    >>
    >> a
    a =
    
        1.00000   15.00000    2.00000    0.50000
    
    >> sum(a)     %所有元素求和
    ans =  18.500
    >> prod(a)    %所有元素求积
    ans =  15
    >> floor(a)     %每个元素向下取整
    ans =
    
        1   15    2    0
    
    >> ceil(a)     %每个元素向上取整
    ans =
    
        1   15    2    1
    
    

     

    >> rand(3)
    ans =
    
       0.61469   0.18858   0.55845
       0.59182   0.66372   0.79564
       0.80858   0.73942   0.54067
    
    >> max(rand(3), rand(3))    %两个矩阵中对应元素取最大值
    ans =
    
       0.71192   0.63884   0.71841
       0.72794   0.89119   0.85213
       0.93154   0.62869   0.21294
    
    >> A
    A =
    
       8   1   6
       3   5   7
       4   9   2
    
    >> max(A,[],1)    %第3个参数是指按列取最大值,第2个参数忽略
    ans =
    
       8   9   7
    
    >> max(A,[],2)    %第3个参数是指按行取最大值,第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
    
    >> 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
    
    >> 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
    
    >> Ainv = pinv(A)    %求逆
    Ainv =
    
       0.147222  -0.144444   0.063889
      -0.061111   0.022222   0.105556
      -0.019444   0.188889  -0.102778
    
    >> A * Ainv    %A乘A逆约等于I
    ans =
    
       1.0000e+00  -1.2157e-14   6.3560e-15
       5.5511e-17   1.0000e+00  -1.5266e-16
      -5.9813e-15   1.2268e-14   1.0000e+00
    
    

    相关文章

      网友评论

          本文标题:2019-02-13 Octave计算数据

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