octave/matlab的基本用法

octave是Stanford ML著名公开课指定的软件。
其语法特性和MATLAB几乎一样。


  • Basic operations
    1. elementary operation/math operation
      • + - * / ^
      • == ~= && || ~ xor
      • 缩进 PS1('>> ')这个缩进是octave独有的。
    2. variable
      • 定义变量a=3; %semicolon supressing output
      • 打印变量disp(a);格式化输出sprintf();字符串用单引号
      • ‘全局格式:format long/format short’。例:a=pi;disp(a);
    3. 矩阵
      • 矩阵和向量的定义
        例: A=[1 2;3 4;5 6]或者
        B=[1 2;
        3 4;
        5 6]
        row vector: v=[1 2 3]
        column vector: c=[1;2;3]
        向量生成式: v=start:step:end; 生成的是row vector,想要转换成column vector转置即可。
      • 特殊矩阵的定义
        • 单位矩阵ones(row,column)
        • 零矩阵zeros(row,column)
        • 随机矩阵rand(row,column)//高斯随机矩阵randn
        • 直方图hist. 例子:w=-6+sqrt(10)*randn(randn(1,10000));hist(w);
        • 单位矩阵eye
    4. help
      例子:help(‘randn’);

  • Move data around
    1. 矩阵的大小&向量的大小
      sizeof(Matrix)
      sizeof(Matrix,1)
      sizeof(Matrix,2)
      length(v)
    2. 加载数据
      load(’filename.dat’)
    3. 查看变量
      who
    4. 清除变量
      clear
    5. 存储变量
      save filename.mat variable;
    6. 读取矩阵中数据
      • 访问元素Matrix(1,1)
      • 访问某行Matrix(1,:)
      • 访问某列Matrix(:,1)
      • Matrix to Vector Matrix(:)
    7. 矩阵的拼接
      C=[A B] or C=[A;B]

  • computing on Data
    1. dot multiply(.*)
    2. .^
    3. ./
    4. log
    5. exp
    6. abs
    7. <
    8. [val ind]=max(vector/matrix)
    9. magic(num)
    10. find
    11. sum sum(A,1) or sum(A,2)
    12. prod
    13. floor
    14. ceil
    15. max(rand(3),rand(3))
    16. max(max(A)) or max(A(:))
    17. flipud
    18. pinv(A)

  • Ploting the Data

    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 'myPlot.png'
    close;
    
    figure(1);plot(t,y1);
    figure(2);plot(t,y2);
    subplot(1,2,1);plot(t,y1);
    subplot(1,2,2);plot(t,y2);
    axis([0.5 1 -1 1]);
    clf;
    
    A=magic(5);
    imagesc(A), colorbar, colormap gray;  //colon can make user input multiple commands in one line
    

  • control statement (for while if define function)
  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值