美文网首页
5. Octave Tutorial(现在一般都用python做

5. Octave Tutorial(现在一般都用python做

作者: 玄语梨落 | 来源:发表于2020-08-16 08:20 被阅读0次

Octave Tutorial

Octave 语法几乎和Matelab一样

  1. 一般的数学计算(elementary math operations)与python几乎一样:+、-、*、\、^
  2. 逻辑运算(logical operations):
    1. ==
    2. ~=
    3. \&\&:AND
    4. ||:OR;xor(0,1)等价于 0||1
    5. $$
  3. \%:comment
  4. PS1('>> '):可以简化命令行成'>> '
  5. disp():打印字符串或者对象等
  6. 向量、矩阵和数组:
    1. v=[1 2;3 4;5 6;]:向量
    2. v=1:0.1:2:从1到2步长位0.1的数组
    3. v=1:6:默认步长为1
    4. 2*ones(2,3):生成2*3的矩阵,都是2,默认ones(2,3)都是1;
    5. zeros(1,3)默认都是0
    6. rand(1,3)随机在0-1之间的数值
    7. randn(3,3)高斯分布的随机值,均值为0,标准差为1
    8. hist(w)打印直方图,w为一个向量,默认为5条,hist(w,n)可以打印出n条来
    9. eye(n)生成一个n维的单位矩阵
  7. help eye生成帮助信息

load data

  1. size(A):returns the size of the matrix A(returns a matrix).size(A,1):the size of the first dimension of A;size(A,2) the number of columms in the matrix A.
  2. length(v):the longer dimension of v.
  3. pwd:show the current path.cd:change a directory.ls:list all the directory of this path.
  4. load featuresX.dat or load('featuresX.dat')
  5. who variables in current scope.whosdetailed view.
  6. clear X get rid of a variable X.clear clear all the variables.
  7. save hello.mat v save the variable v to hello.mat.This commend will save data in a binary format.save hello.txt v -ascii save data in a text format.
  8. A(:,2) the colon (:) means every elemet along the second column.A([1 3],:) get everything from the first row and the third row.A(:,2) = [10;11;12] assigment the sencond column of the matrix. A = [A,[10,11,12]] append a column in the right.
  9. A(:)put all elements of A into a single vector.

Computing on data

  1. A*C: matrix mulitipation.
  2. A.*B: elements wise operations
  3. A./B:
  4. 1./B: the dot means emement wise operations in octave.
  5. A.^2: the square of each elemnets.
  6. log(v):
  7. abs(v): the absolute value of each element in v.
  8. exp(v):
  9. -v=-1*v:
  10. v+ones(length(v),1)=v+1:
  11. A': a transpose of A.
  12. val=max(A): the column wise maximum.
  13. [val,ind]=max(A): the maximum and the index.
  14. A < 2: elements wise compariation(0 or 1).false or true.
  15. find(A<2): return the index of which is true less then 2.
  16. magic(3): create an N-by-N magic square.All the rows columns and diagnoals sum up to the same thing.
  17. (r,c) = find(A>1): returnes the rows and columns
  18. sum(A): return the sum of A.prod(a): returns the product of A.floor(A): rounds down all the elemments of A. ceil(A): rounds up .
  19. max(rand(3),rand(3)): returns a ramdomly matrix ,each element is the maxium of two radomly generated matrices.
  20. max(A,[],1): the max of each column. This 1 means take the max along the first dimension of A.
  21. flipup(A): flip up the matrix A.

Plotting data

When using ';', there will be no data show in the cmd.

example:

t=[0;0.01;0.98];
y1=sin(2*pi*4*t);
plot(t,y1);
y2=cos(2*pi*4*t);
plot(t,y2);
plot(t,y1);
hold on;
plot(t,y2,'r');
xlabel('time');
ylabel('value);
legend('sin','cos')
title('my plot')
print -dpng 'my plot.png'

figure(1);plot(t,y1);
figure(2);plot(t,y2);% 可以出现两张图
subplot(1,2,1);% Divides plot a 1x2 grid, access first element.
axis([0.5 1 -1 1]) % set the x range and y range for the figure 
clf
A = magic(5)
imagesc(A)
imagesc(A),colorbar,colormap gray;

clf: clear a fiugere.
close all: close all fiugres.

a=1,b=2,c=3;% comman chaining of function calls.

For, while, if statements, and functions

v=zeros(10,1)
for i-1:10,
   v(i)=^i;
end;

indices=1:10;
for i=indices,
   disp(i);
end;

i=1;
while 9<=5,
   v(i)=100;
   i=i+1;
end;

i=1;
while true,
   v(i)= 999;
   i=i+1;
   if i == 6,
      break;
   end;
end;

v(1)=2;
if v(i)===1,
   disp('The value is 1');
elseif v(i)==2,
   disp('The value is 2');
else
   disp('The value is not one or two.');
end;

定义函数:

  • 文件名要与函数名一致,后缀为.m
  • 第一行function y = functionName(x)
  • 第三行函数体y = x^2;,这是一个平方函数。
  • 使用的时候,需要用cd打开目录,或者用addpath('path')把路径加入进去。
  • 可以定义返回多个变量的函数function [y1,y2] = functionName(x)

一个简单的代价函数计算函数:

function J = costFunctionJ(x,y,theta)
% x is the "design matrix" containing our training examples.
% y is the class labels.

m = size(x,1); % nmuber of training examples
predictions = x*theta; % predictions of hypothesis on all m examples
sqrErrors = (predictions-y).^2; % squared errors
J = 1/(2*m) * sum(sqrErrors);


Vectorization

  1. example:
    h_\theta(x)=\sum_{j=0}^n\Theta_jx_j=\Theta^Tx

\Theta=\begin{bmatrix} \Theta_0 \\ \Theta_1 \\ \Theta_2 \\ \end{bmatrix} \quad x=\begin{bmatrix} x_0 \\ x_1 \\ x_2 \end{bmatrix}

  1. example2:两个变量的预测
    \Theta_0 := \Theta_0 - \alpha\frac{1}{m}\sum_{i=1}^m(h_\Theta(x^{(i)})-y^{(i)})x_0^{(i)} \newline \Theta_1 := \Theta_1 - \alpha\frac{1}{m}\sum_{i=1}^m(h_\Theta(x^{(i)})-y^{(i)})x_1^{(i)} \newline \Theta_2 := \Theta_2 - \alpha\frac{1}{m}\sum_{i=1}^m(h_\Theta(x^{(i)})-y^{(i)})x_2^{(i)}

相关文章

网友评论

      本文标题:5. Octave Tutorial(现在一般都用python做

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