[Machine Learning]吴恩达机器学习笔记 五——Octave/Matlab教程

1、基本操作

% 注释
== 全等于
~= 约等于
&& and并
|| or或
xor 异或

PS1('>> ') 修改提示符为>>, 可修改成其他
; 阻止打印输出
'' 单引号字符串
disp() 打印输出
format long 输出位数为long
format short 输出位数为short 还有其他的
format xxx 格式输出

矩阵
>>A = [1 2; 3 4; 5 6]
A =

   1   2
   3   4
   5   6

>>v = [1 2 3]
v =

   1   2   3

>>c = [1; 2;3]
c =

   1
   2
   3
>>v = 1:0.1:2
v =

 Columns 1 through 5:

    1.0000    1.1000    1.2000    1.3000    1.4000

 Columns 6 through 10:

    1.5000    1.6000    1.7000    1.8000    1.9000

 Column 11:

    2.0000
>>eye(3) %对角矩阵
ans =

Diagonal Matrix

   1   0   0
   0   1   0
   0   0   1

>>v = 1: 6
v =

   1   2   3   4   5   6
>>ones(2, 3)
ans =

   1   1   1
   1   1   1

>>c = 2 * ones(2, 3)
c =

   2   2   2
   2   2   2
>>zeros(2, 4)
ans =

   0   0   0   0
   0   0   0   0
>>w = rand(1, 3)
w =

   0.824123   0.536888   0.012791

>>rand(3, 3)
ans =

   0.155764   0.548978   0.872017
   0.025111   0.886286   0.538810
   0.564373   0.977757   0.798323
>>w = randn(1, 3) %高斯随机变量
w =

  -0.44255   0.51773   0.26950

>>w = -6 + sqrt(10)*(randn(1, 100000));
>>hist(w)    %绘图
>>hist(w, 50)

2. 移动数据

>>A
A =

   1   2
   3   4
   5   6

>>size(A)
ans =

   3   2

>>size(A, 1)
ans =  3
>>size(A, 2)
ans =  2
>>v
v =

   1   2   3   4   5   6

>>length(v)
ans =  6

who 显示octave内存中的变量
>>who
Variables in the current scope:

A    ans  c    v    w

whos显示octave内存中更详细的变量信息
>>whos
Variables in the current scope:

   Attr Name        Size                     Bytes  Class
   ==== ====        ====                     =====  =====
        A           3x2                         48  double
        ans         1x11                        11  char
        c           2x3                         48  double
        v           1x6                         24  double
        w           1x100000                800000  double

Total is 100029 elements using 800131 bytes

>>clear 清除内存中的所有变量

>>A
A =

   1   2
   3   4
   5   6

>>b = ones(3, 2)
b =

   1   1
   1   1
   1   1

>>c = [A b]
c =

   1   2   1   1
   3   4   1   1
   5   6   1   1

>>c = [A; b]
c =

   1   2
   3   4
   5   6
   1   1
   1   1
   1   1

3. 计算数据

>>A
A =

   1   2
   3   4
   5   6

>>B = ones(3, 2)
B =

   1   1
   1   1
   1   1

>>A .*B
ans =

   1   2
   3   4
   5   6

>>A .^2
ans =

    1    4
    9   16
   25   36

>>V = [1; 2; 3]
V =

   1
   2
   3

>>1 ./V
ans =

   1.00000
   0.50000
   0.33333

>>log(V)
ans =

   0.00000
   0.69315
   1.09861

>>exp(V)
ans =

    2.7183
    7.3891
   20.0855

>>abs([-1; -3; 2])
ans =

   1
   3
   2

>>-V
ans =

  -1
  -2
  -3

>>V + ones(length(V), 1)
ans =

   2
   3
   4

>>A
A =

   1   2
   3   4
   5   6

>>A'
ans =

   1   3   5
   2   4   6

>>a = [1 15 2 0.5]
a =

    1.00000   15.00000    2.00000    0.50000

>>max(a)
ans =  15
>>a < 3
ans =

  1  0  1  1

>>find(a < 3)
ans =

   1   3   4

>>a = magic(3)    %幻方矩阵
a =

   8   1   6
   3   5   7
   4   9   2

>>[r, c] = find(a >=7)
r =

   1
   3
   2

c =

   1
   2
   3

>>a
a =

   8   1   6
   3   5   7
   4   9   2

>>sum(a)
ans =

   15   15   15

>>prod(a) %乘积
ans =

   96   45   84


>>a = [1 15 2 0.5]
a =

    1.00000   15.00000    2.00000    0.50000

>>floor(a) %向下取整
ans =

    1   15    2    0

>>ceil(a) %向上取整
ans =

    1   15    2    1

>>A
A =

   1   2
   3   4
   5   6

>>pinv(A)    %伪逆矩阵
ans =

  -1.33333  -0.33333   0.66667
   1.08333   0.33333  -0.41667

4、数据绘制

>>t = [0:0.01: 0.98];
>>y1 = sin(2 * pi * 4 * t);
>>plot(t, y1)
>>y2 = cos(2 * pi * 4 * t);
>>plot(t, y2)
>>hold on %保存原来的画布,下面的绘图在上一个图上绘制
>>plot(t, y1, 'r')

​

 

 

 

 

 

 

 

 

 

添加标签

>>xlabel('time')
>>ylabel('value')
>>legend('sin', 'cos')
>>title('my plot')
>>print -dpng 'myPlot.png' % 保存图片,可以换成其他文件格式,

保存时最好切换到指定路径下保存
>>pwd %查看当前路径
ans = C:\Users\mi\Desktop
>>cd C:\Users\mi\Desktop %切换路径

>>close %关闭,显示图片的窗口会消失

>>close

>>figure(1): plot(t, y1)    %可同时绘制多个图片
ans = [](1x0)
>>figure(2): plot(t, y2)
ans = [](1x0)

>>subplot(1, 2, 1) %将图片分成1行两个格子,正在使用的是第一个格子

>>subplot(1,2,1)
>>plot(t, y1)
>>subplot(1,2,2)
>>plot(t, y2, 'r')
>>axis([0.5 1 -1 1])    %修改坐标轴x为0.5到1,y为-1到1

>>clf    %清空画布上的图片
>>
>>A = magic(5)
A =

   17   24    1    8   15
   23    5    7   14   16
    4    6   13   20   22
   10   12   19   21    3
   11   18   25    2    9

>>imagesc(A) 
>>>>imagesc(A), colorbar, colormap gray;     %逗号连用函数
>>

 

5、控制语句:for、while、if

>>for i = 1: 10,
> v(i) = 2^i;
> end
>>v
v =

      2
      4
      8
     16
     32
     64
    128
    256
    512
   1024

i = 1;
>>while i <= 5,
>
Display all 1729 possibilities? (y or n)
>
> v(i) = 100;
> i += 1;
> end;
>>v
v =

    100
    100
    100
    100
    100
     64
    128
    256
    512
   1024


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

    999
    999
    999
    999
    999
     64
    128
    256
    512
   1024

>>v(1) = 2
v =

      2
    999
    999
    999
    999
     64
    128
    256
    512
   1024

>>if v(1) == 1,
>  disp('The value is one');
> elseif v(1) == 2,
>  disp('The value is two');
> else
>  disp('the valueis not one or two');
> end;
ans = 0
The value is two
>>

上面是一些控制语句的使用,语法和python或者其他很多高级编程语言类似不必多说,看几遍差不多就能够会用了。

那么下面主要来说一下在octave/matlab中如何自定义函数来使用

首先,把函数写在单独的文件里面,并以函数名.m保存文件

我这里直接保存在桌面上了,然后到这个路径下才能使用这个函数。

ans = C:\Users\mi\Desktop
>>sequareThisNumber(5)
ans =  25
>>

还可以将这个路径添加到octave的搜索路径中去,使用addpath()命令,这样在其他路径下都能搜索到使用了

>>addpath('C:\Users\mi\Desktop')
>>cd C
error: C: No such file or directory
>>cd C:
>>pwd
ans = C:\
>>sequareThisNumber(5)
ans =  25
>>

另外,octave 自定义函数可以返回多个结果,这个与其他的高级编程语言不同 

>>[a, b] = squareAndCubeThisNumber(3)
a =  9
b =  27

最后是代价函数的示例,

>>X = [1 1; 1 2; 1 3]
X =

   1   1
   1   2
   1   3

>>y = [1; 2; 3]
y =

   1
   2
   3

>>theta = [0;1]
theta =

   0
   1

>>j = costFunctionJ(X, y, theta)
j = 0

这个代价函数是过原点的一次正比函数,所以最小值就是0,非常的简单。

 

6、矢量

实际上,我们可以发现,循环同样能够实现向量的功能,但向量的运用能帮助我们计算更加快速,代码量减少,同时代码更加容易理解。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值