顯示具有 octave 標籤的文章。 顯示所有文章
顯示具有 octave 標籤的文章。 顯示所有文章

2016年7月11日 星期一

[octave] install octave 4.0.3 under mac osx EI 10.11, plot OK


ref: https://kiskeyix.org/articles/605

1.
install hombrew
install AquaTerm from sourceforge
brew tap homebrew/science
brew reinstall gnuplot --with-aquaterm
gnuplot # make sure it says "terminal set to aqua"
brew install lua51 # yes, you also need this old version of Lua
brew install octave

2.
vim ~/.octaverc
add below....

octave:1> graphics_toolkit('gnuplot')
octave:1> setenv('GNUTERM','aqua')

check again

octave:1> getenv("GNUTERM")
ans = aqua
octave:2>



=========================
For waring message when you plot
warning: could not match any font: *-normal-normal-10


use...

brew uninstall fontconfig

brew install fontconfig --universal


OK












2016年5月23日 星期一

[octave] element by element operation

      plus       .+
      minus     .-
      times     .*
      rdivide   ./
      ldivide   .\
      power     .^  .**

https://www.gnu.org/software/octave/doc/v4.0.0/Arithmetic-Ops.html#Arithmetic-Ops

x .+ y

    Element-by-element addition. This operator is equivalent to +.



x = [1 2 3;
     4 5 6;
     7 8 9]

y = [1 1 1;
     1 1 1;
     1 1 1]

x.+y # equal x+y, because + originally is a element by element operator

ans =

    2    3    4
    5    6    7
    8    9   10



x .* y

    Element-by-element multiplication. If both operands are matrices, the number of rows and columns must both agree, or they must be broadcastable to the
same shape.

x = [1 2 3;
     4 5 6;
     7 8 9]

y = [10 2 1;
     10 2 1;
     10 2 1]

x*y #normal *

ans =

    60    12     6
   150    30    15
   240    48    24

x.*y # element by element 1*10 4*10 7*10 .....
ans =

   10    4    3
   40   10    6
   70   16    9



x ./ y

    Element-by-element right division.




x = [1 2 3;
     4 5 6;
     7 8 9]

y = [10 2 1;
     10 2 1;
     10 2 1]

x./y
ans =

   0.10000   1.00000   3.00000
   0.40000   2.50000   6.00000
   0.70000   4.00000   9.00000




y = [10 2 1;
     10 2 1;
     10 2 1]
m=5
y./5 # equal y/5

ans =

   2.00000   0.40000   0.20000
   2.00000   0.40000   0.20000
   2.00000   0.40000   0.20000










[octave]broadcasting

https://www.gnu.org/software/octave/doc/v4.0.0/Broadcasting.html#Broadcasting


在octave 中, broadcasting 的意思是
當兩個唯度不同矩陣要運算時, 維度小的 會自己擴充成跟維度大的一樣維度之後, 才進行運算


ex:

x = [1 2 3;
     4 5 6;
     7 8 9]

y = [10 20 30]

x+y  # 這本來是不能作的, 因為x y 維度不同, 但他自動幫你變成

y = [10 20 30
     10 20 30
     10 20 30];

這就是broadcasting, 此時 y 維度就跟x 一樣, 可以作加法了

>>output
warning: operator +: automatic broadcasting operation applied
ans =

   11   22   33
   14   25   36
   17   28   39


(他也會出現警告, 提醒你這個+法經過了broadcasting)



 如果兩個matrix 一樣dimension , 則直接就是
element by element 相加



x = [1 2 3;
     4 5 6;
     7 8 9]

y = [1 2 3;
     4 5 6;
     7 8 9]

x+y
ans =

    2    4    6
    8   10   12
   14   16   18







[octave ] octave zeros function

Built-in Function: zeros (n)
Built-in Function: zeros (m, n)
Built-in Function: zeros (m, n, k, …)
Built-in Function: zeros ([m n …])
Built-in Function: zeros (…, class)

    Return a matrix or N-dimensional array whose elements are all 0.

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

    If invoked with two or more scalar integer arguments, or a vector of integer values, return an array with the given dimensions.

    The optional argument class specifies the class of the return array and defaults to double. For example:

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


a=[1, 2, 33;4 ,5, 6; 7 ,8, 66;55 ,476, 22]
a =

     1     2    33
     4     5     6
     7     8    66
    55   476    22

[rows columns]=size(a);
rows =  4
columns =  3

zeros (rows,columns, "uint8")

ans =

  0  0  0
  0  0  0
  0  0  0
  0  0  0


zeros(4) # will give you 4x4 matrix
ans =

   0   0   0   0
   0   0   0   0
   0   0   0   0
   0   0   0   0


zeros(4,1) #want a 4x1 zero vector
ans =

   0
   0
   0
   0






[octave] size function

size function

https://www.gnu.org/software/octave/doc/v4.0.1/Object-Sizes.html#XREFsize


Built-in Function: size (a)
Built-in Function: size (a, dim)

    Return the number of rows and columns of a.

    With one input argument and one output argument, the result is returned in a row vector. If there are multiple output arguments, the number of rows is assigned to the first, and the number of columns to the second, etc. For example:

    size ([1, 2; 3, 4; 5, 6])
       ⇒ [ 3, 2 ]

    [nr, nc] = size ([1, 2; 3, 4; 5, 6])
        ⇒ nr = 3
        ⇒ nc = 2

    If given a second argument, size will return the size of the corresponding dimension. For example,

    size ([1, 2; 3, 4; 5, 6], 2)
        ⇒ 2

    returns the number of columns in the given matrix.

    
Testing:
    
a=[1, 2, 33;4 ,5, 6; 7 ,8, 66;55 ,476, 22]
a =

     1     2    33
     4     5     6
     7     8    66
    55   476    22

[rows columns]=size(a); 
rows =  4
columns =  3


size(a,1) # 1 means get rows ;  2 means get columns
ans =  4


size(a,2)
ans =  3



-------------------------------------------------------------------------------









 

[Octave] Octave matrix slice

CASE1:

a=[1, 2, 33;4 ,5, 6; 7 ,8, 66];

a =

    1    2   33
    4    5    6
    7    8   66

a(3)       # result is a scalar
 ans =  7


a(1:4)     # show range form index 1 to(:) index 4, and result is a row vector
ans =

   1   4   7   2

a([1; 9])  # show range form index 1 and index 9 result is a column vector
ans =

    1
   66

a(1, [1, 3])  # row 1, columns 1 and 3
ans =

    1   33


a(3, 1:3)     # row 3, columns in range 1-2
ans =

    7    8   66

a(1, :)       # row 1, all columns , use really often !!!

ans =

    1    2   33


Case2:

a = [1, 2, 3, 4]
a =

   1   2   3   4

a(1:end/2)        # first half of a => [1, 2],end is the last element in the matrix
ans =

   1   2


a(end + 1) =5   # append element
 a =

   1   2   3   4   5

a(end) = []      # delete element
a =

   1   2   3   4

a(1:2:end)        # odd elements of a => [1, 3]
ans =

   1   3

a(2:2:end)        # even elements of a => [2, 4]
ans =

   2   4

a(end:-1:1)       # reversal of a => [4, 3, 2 , 1]
ans =

   4   3   2   1



 CASE3: often use in machine learning cost function

a=[1, 2, 33;4 ,5, 6; 7 ,8, 66];

a =

    1    2   33
    4    5    6
    7    8   66

[99 ; a(1,:)']

 ans =

   99
    1
    2
   33


num_labels=4;
zeros(num_labels,1)

ans =

   0
   0
   0
   0


a=[1, 2, 33;4 ,5, 6; 7 ,8, 66;55 ,476, 22]
a =

     1     2    33
     4     5     6
     7     8    66
    55   476    22

a(:,2:end) #from second column  to end

ans =

     2    33
     5     6
     8    66
   476    22

a(1,:) #get first row
ans =

    1    2   33

a(2:end) # start from element 2 to end

ans =

     4     7    55     2     5     8   476    33     6    66    22

a(:) # unroll every element


ans =

     1
     4
     7
    55
     2
     5
     8
   476
    33
     6
    66