Computation Operation

  • "*"
octave:7> A
A =

   1   2
   3   4
   5   6

octave:8> B
B =

   11   12
   13   14
   15   16

octave:9> C
C =

   1   1
   2   2

octave:10> A * C  
ans =

    5    5
   11   11
   17   17

  • ".": element wise operation
octave:11> A .* B
ans =

   11   24
   39   56
   75   96

octave:12> A .^2
ans =

    1    4
    9   16
   25   36
  • Transpose: '
octave:13> A'
ans =

   1   3   5
   2   4   6
  • max
octave:28> a = [1 15 2 0.5]
a =

    1.00000   15.00000    2.00000    0.50000

octave:29> val = max(a)
val =  15
octave:30> [val, ind] = max(a)
val =  15
ind =  2
octave:31> A
A =

   1   2
   3   4
   5   6

octave:32> max(A)
ans =

   5   6
--------------------------------------------
octave:38> temp1 = [1 10; 2 11; 3 12];
octave:39> temp2 = [10 1; 20 21; 30 3];
octave:40> max(temp1, temp2)
ans =

   10   10
   20   21
   30   12
--------------------------------------------
octave:42> A
A =

   8   1   6
   3   5   7
   4   9   2

octave:43> max(A, [], 1)
ans =

   8   9   7

octave:44> max(A, [], 2)
ans =

   8
   7
   9
---------------------- % to find the max value in the matrix
octave:45> max(max(A))
ans =  9
octave:46> max(A(:))
ans =  9
  • find
octave:34> A = magic(3)
A =

   8   1   6
   3   5   7
   4   9   2

octave:35> [r, c] = find(A > 7)
r =

   1
   3

c =

   1
   2

  • sum && prod && flipud
octave:36> sum(a)
ans =  18.500
octave:37> prod(a)
ans =  15
-------------------------------------% to sum up the diagonal element
octave:47> 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

octave:48> sum(A, 1)
ans =

   369   369   369   369   369   369   369   369   369

octave:49> sum(A, 2)
ans =

   369
   369
   369
   369
   369
   369
   369
   369
   369

octave:50> 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

octave:51> sum(A.*eye(9))
ans =

   47   68    8   20   41   62   74   14   35

octave:52> sum(sum(A.*eye(9)))
ans =  369
-----------------------------------------------%to sum up the opposite diagonal
octave:53> 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

octave:54> sum(sum(A .* flipud(eye(9))))
ans =  369
  • pinv
octave:55> A = magic(3)
A =

   8   1   6
   3   5   7
   4   9   2

octave:56> pinv(A)
ans =

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

octave:57> A * pinv(A)
ans =

   1.0000e+00  -1.1713e-14   6.3283e-15
   4.1633e-17   1.0000e+00  -1.1102e-16
  -5.9119e-15   1.2490e-14   1.0000e+00

你可能感兴趣的:(Computation Operation)