• CheeseZH: Octave basic commands


      1 1.Basic Operations
      2 5+6
      3 3-2
      4 5*8
      5 1/2
      6 2^6
      7 1 == 2 %false  ans = 0
      8 1 ~= 2 %true ans = 1
      9 1 && 0 %AND ans = 0
     10 1 || 0 %OR ans = 1
     11 xor(1,0) %ans = 1
     12 PS1('>> '); %change the command prompt info
     13 a = 3 %show a = 3 in screen
     14 a = 3 %not show a = 3 in screen
     15 b = 'hi'
     16 c = (3>=1)
     17 a = pi; %a = 3.1416
     18 disp(sprintf('2 decimals: %0.2f',a)) %2 decimals: 3.14
     19 disp(sprintf('6 decimals: %0.6f',a)) %2 decimals: 3.141593
     20 format long  %a = 3.14159265358979
     21 format short %a = 3.1416
     22 
     23 2. Matrices and Vectors
     24 A = [1 2; 3 4; 5 6]
     25 v = [ 1 2 3]
     26 v = [1; 2; 3]
     27 v = 1:0.1:2 %1.0 1.1 1.2 ... 2.0
     28 v = 1:6
     29 v = ones(2,3)
     30 c = 2*ones(2,3)
     31 w = ones(1,3)
     32 w = zeros(1,3)
     33 w = rand(1,3)
     34 w = rand(3,3)
     35 w = randn(1,3) %Gossian distribution
     36 w = -6 + sqrt(10)*(randn(1,10000))
     37 hist(w) %draw a histogram
     38 hist(w,50) 
     39 I = eye(4)
     40 help eye
     41 help rand
     42 help help
     43 
     44 3.Moving data around
     45 A = [1 2; 3 4; 5 6]
     46 size(A) %ans = 3 2
     47 size(A,1) %ans = 3
     48 size(A,2) %ans = 2
     49 A(3,2) %ans = 6
     50 A(2,:) %the second row, ':' means every elements along that row/column
     51 A(:,2) %the second column
     52 A([1 3],:) %the first and the third row
     53 A(:,2) = [10; 11; 12]
     54 A = [A, [100; 101; 102]] %append another column vector to right
     55 A(:) %put all elements of A into a single vector
     56 A = [1 2; 3 4; 5 6]
     57 B = [11 12; 13 14; 15 16]
     58 C = [A B] %3 * 4
     59 C = [A; B] %6 * 2
     60 
     61 
     62 v = [1 2 3 4]
     63 length(v) %ans = 4
     64 length(A) %ans = 3
     65 length([1;2;3;4;5]) %ans = 5
     66 
     67 pwd %current path
     68 cd 'path...' 
     69 ls
     70 
     71 load featuresX.dat
     72 load priceY.dat
     73 load('featuresX.dat')
     74 load('priceY.dat')
     75 
     76 who %show the variables in current scope
     77 whos %show the variables detail in current scope
     78 size(featuresX) %ans = 47 2
     79 
     80 v = priceY(1:10)
     81 
     82 save hello.mat v; %save v into a file named "hello.mat"(BINARY)
     83 save hello.txt v; %txt format can be understood by human(ASCII)
     84 clear featuresX %delete featuresX from current scope
     85 clear %delete all variables in current scope
     86 
     87 4.Computing on data
     88 A = [1 2; 3 4; 5 6]
     89 B = [11 12; 13 14; 15 16]
     90 C = [1 1; 2 2]
     91 A*C
     92 A.*B %'.' element operation
     93 A.^2
     94 v = [1; 2; 3]
     95 1./v
     96 log(v)
     97 exp(v)
     98 abs(v)
     99 -v %-1*v
    100 v + ones(length(v),1) % v + 1, v + ones(3,1)
    101 A' %transpose
    102 
    103 a = [1 15 2 0.5]
    104 val = max(a) %val = 15
    105 [val, ind] = max(a) %val=15 ind(ex)=2
    106 max(A) %ans = 5 6
    107 a < 3 %element wise comparison: ans = 1 0 1 1
    108 find(a<3) %ans = 1 3 4
    109 A = magic(3)
    110 [r, c] = find(A>=7) % r = 1; 3; 2   c = 1; 2; 3 ==>A(1,1) A(3,2) A(2,3) are greater than 7
    111 help find
    112 sum(a) 
    113 prod(a)
    114 floor(a)
    115 ceil(a)
    116 max(rand(3),rand(3))
    117 max(A,[],1) %the maximum element in each column
    118 max(A,[],2) %the maximum element in each row
    119 max(A) %ans = 8 9 7
    120 max(max(A)) %ans = 9
    121 max(A(:)) %ans = 9
    122 A = magic(9)
    123 sum(A,1) %sum up each column 
    124 sum(A,2) %sum up each row
    125 sum(sum(A.*eye(9))) %sum up the main diagonal
    126 sum(sum(A.*flipud(eye(9)))) %sum up the other diagonal
    127 pinv(A) %pseudo inverse
    128 
    129 
    130 5. Plotting Data
    131 t = [0:0.01:0.98]
    132 y1 = sin(2*pi*4*t)
    133 plot(t,y1);%sin function
    134 y2 = cos(2*pi*4*t)
    135 hold on; %put figures in one window
    136 plot(t,y2,'r') %change the color of cos to red
    137 xlabel('time')
    138 ylabel('value')
    139 legend('sin','cos')
    140 title('my plot')
    141 cd '/home/zhanghe';
    142 print -dpng 'myPlot.png'
    143 close
    144 figure(1);plot(t,y1);
    145 figure(2);plot(t,y2);
    146 subplot(1,2,1) %%divides plot into 1*2 grid access first element
    147 plot(t,y1);
    148 subplot(1,2,2) %%divides plot into 1*2 grid access second element
    149 plot(t,y2);
    150 axis([0.5 1 -1 1])
    151 clf;
    152 A = magic(5)
    153 imagesc(A)
    154 imagesc(A),colorbar,colormap
    155 
    156 
    157 6. Control statements
    158 v = zeros(10,1)
    159 for i=1:10,
    160   v(i) = 2^i;
    161 end;
    162 indices = 1:10;
    163 for i=indices,
    164   disp(i);
    165 end;
    166 i = 1;
    167 while i<=5,
    168   v(i) = 100;
    169   i = i+1;
    170 end;
    171 while true,
    172   v(i) = 999;
    173   i = i+1;
    174   if i==6,
    175     break;
    176   end;
    177 end;
    178 v(1) = 2;
    179 if v(1) == 1,
    180   disp('One');
    181 elseif v(1) == 2,
    182   disp('Two');
    183 else
    184   disp('Else');
    185 end;
    186 
    187 %Octave search path (advanced/optional)
    188 addpath('/home/zhanghe/ml/ex1')
    189 
    190 %Example of CostFunction
    191 predictions = X*theta;
    192 sqrErrors = (predictions-y).^2;
    193 J = 1/(2*m) * sum(sqrErrors);
    194 
    195 7.Vectorization
    196 % Hypothesis Function
    197 % Unvectorized implementation
    198 prediction = 0.0;
    199 for j = 1:n+1,
    200   prediction = prediction + theta(j) * x(j)
    201 end;
    202 % Vectorized implementation
    203 prediction = theta' * x
    204 
    205 % Gradient descent(Simultaneous updates)
    206 % Vectorized implementation
    207 delta = 1/m*((hypothesis-y)*x)
    208 theta = theta - alpha*delta
  • 相关阅读:
    MYsql 之单标查询.
    MYSQL社区版安装手册
    爬虫框架学习
    celery_消息队列
    rabbitMQ消息队列
    Django分页设置
    Mysql-基础+安装指南
    super函数的用法
    hashlib 和loggin模块
    Python的魔法方法
  • 原文地址:https://www.cnblogs.com/CheeseZH/p/4596782.html
Copyright © 2020-2023  润新知