Octave教程二:Moving Data Around

Octave教程二:Moving Data Around

在本篇文章中,我将介绍如何在Octave中移动数据,具体来说,如果你有一个机器学习问题,你怎样把数据加载到Octave中。怎样把数据存入一个矩阵、如何对矩阵进行相乘、如何保存计算结果、如何移动这些数据,并用数据进行操作……

代码

>>
>> A
A =

   1   2
   3   4
   5   6

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

   1   2
   3   4
   5   6

>> size(A)
ans =

   3   2

>> sz = size(A)
sz =

   3   2

>> size(sz)
ans =

   1   2

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

   1   2   3   4

>> length(v)
ans =  4
>> A
A =

   1   2
   3   4
   5   6

>> length(A)
ans =  3
>>
>> length([1;2;3;4;5])
ans =  5
>>

length返回矩阵的最大维度,但通常我们还是对向量使用length命令。
下面我们来看一下,如何在系统中加载数据和寻找数据。
当我们打开Octave时,我们通常已经在一个默认路径中,这个路径是Octave的安装位置。pwd命令可以显示出Octave当前所处路径。

>> pwd
ans = C:\Users\huyang
>> cd 'D:\soft\Octave\user'
>> ls
 驱动器 D 中的卷是 本地磁盘1
 卷的序列号是 000A-115B

 D:\soft\Octave\user 的目录

[.]             [..]            featuresX.dat   priceY.dat
               2 个文件            373 字节
               2 个目录 38,748,803,072 可用字节
>> load featuresX.dat
>> load priceY.dat
>> load ('featuresX.dat')
>>

另外,who命令能够显示出,在我的Octave工作空间中的所有变量;
whos命令能更详尽地查看

>> who
Variables in the current scope:

A          C          V          W          a          ans        b          c          featuresX  priceY     sz         v

>> featuresX
featuresX =

   2104      3
   1600      3
   2400      3
   1416      2
   3000      4
   1985      4
   1534      3
   1427      3
   1380      3
   1494      3
   1940      4
   2000      3
   1890      3
   4478      5
   1268      3
   1437      3
   1239      3
   2132      4
   4215      4
   2162      4
   1664      2
   2238      3
   2567      4
   1200      3
    852      2
   1852      4
   1203      3

>> size(featuresX)
ans =

   27    2

>> size(priceY)
ans =

   27    1

>> whos
Variables in the current scope:

   Attr Name           Size                     Bytes  Class
   ==== ====           ====                     =====  =====
        A              3x2                         48  double
        C              3x4                         96  double
        V              1x6                         24  double
        W              1x10000                  80000  double
        a              1x1                          8  double
        ans            1x2                         16  double
        b              1x5                          5  char
        c              1x1                          1  logical
        featuresX     27x2                        432  double
        priceY        27x1                        216  double
        sz             1x2                         16  double
        v              1x4                         32  double

Total is 10120 elements using 80894 bytes

>> clear featuresX
>> whos
Variables in the current scope:

   Attr Name        Size                     Bytes  Class
   ==== ====        ====                     =====  =====
        A           3x2                         48  double
        C           3x4                         96  double
        V           1x6                         24  double
        W           1x10000                  80000  double
        a           1x1                          8  double
        ans         1x2                         16  double
        b           1x5                          5  char
        c           1x1                          1  logical
        priceY     27x1                        216  double
        sz          1x2                         16  double
        v           1x4                         32  double

Total is 10066 elements using 80462 bytes

>> load featuresX.dat
>> whos
Variables in the current scope:

   Attr Name           Size                     Bytes  Class
   ==== ====           ====                     =====  =====
        A              3x2                         48  double
        C              3x4                         96  double
        V              1x6                         24  double
        W              1x10000                  80000  double
        a              1x1                          8  double
        ans            1x2                         16  double
        b              1x5                          5  char
        c              1x1                          1  logical
        featuresX     27x2                        432  double
        priceY        27x1                        216  double
        sz             1x2                         16  double
        v              1x4                         32  double

Total is 10120 elements using 80894 bytes

>> V = priceY(1:10)
V =

   3999
   3299
   3690
   2320
   5399
   2999
   3149
   1989
   2120
   2425

>> whos
Variables in the current scope:

   Attr Name           Size                     Bytes  Class
   ==== ====           ====                     =====  =====
        A              3x2                         48  double
        C              3x4                         96  double
        V             10x1                         80  double
        W              1x10000                  80000  double
        a              1x1                          8  double
        ans            1x2                         16  double
        b              1x5                          5  char
        c              1x1                          1  logical
        featuresX     27x2                        432  double
        priceY        27x1                        216  double
        sz             1x2                         16  double
        v              1x4                         32  double

Total is 10124 elements using 80950 bytes

>> save hello.mat V;
>> clear
>> whos
>> load featuresX.dat
>> load priceY.dat
>> whos
Variables in the current scope:

   Attr Name           Size                     Bytes  Class
   ==== ====           ====                     =====  =====
        featuresX     27x2                        432  double
        priceY        27x1                        216  double

Total is 81 elements using 648 bytes

>> loda hello.mat
error: 'loda' undefined near line 1 column 1
>> load hello.mat
>> whos
Variables in the current scope:

   Attr Name           Size                     Bytes  Class
   ==== ====           ====                     =====  =====
        V             10x1                         80  double
        featuresX     27x2                        432  double
        priceY        27x1                        216  double

Total is 91 elements using 728 bytes

>> V
V =

   3999
   3299
   3690
   2320
   5399
   2999
   3149
   1989
   2120
   2425

>> save hello.txt v -ascii   % save as text (ASCII)
>> save hello.txt V -ascii   % save as text (ASCII)
>> A = [1 2;3 4;5 6]
A =

   1   2
   3   4
   5   6

>> A(3,2)
ans =  6
>> A(:,2)
ans =

   2
   4
   6

>> A(,:)
parse error:

  syntax error

>>> A(,:)
      ^

>> A(2,:)
ans =

   3   4

>> A([1 3],:)
ans =

   1   2
   5   6

>> A(:,2) = [10 11 12]
A =

    1   10
    3   11
    5   12

>> A
A =

    1   10
    3   11
    5   12

>> A = [A,[100 101 102]]
error: horizontal dimensions mismatch (3x2 vs 1x3)
>> A = [A,[100, 101, 102]]
error: horizontal dimensions mismatch (3x2 vs 1x3)
>> A = [A,[100, 101, 102]];
error: horizontal dimensions mismatch (3x2 vs 1x3)
>> A = [A,[100; 101; 102]];
>> A
A =

     1    10   100
     3    11   101
     5    12   102

>> A = [A,[200 101 102]];
error: horizontal dimensions mismatch (3x3 vs 1x3)
>> A = [[200 201 202],A];
error: horizontal dimensions mismatch (1x3 vs 3x3)
>> A(:)    % put all elements of A into a single vector
ans =

     1
     3
     5
    10
    11
    12
   100
   101
   102

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

   1   2
   3   4
   5   6

>> B = [12 13;14 15;16 17]
B =

   12   13
   14   15
   16   17

>> C = [A B]
C =

    1    2   12   13
    3    4   14   15
    5    6   16   17

>> C = [A;B ]
C =

    1    2
    3    4
    5    6
   12   13
   14   15
   16   17

>> [A B]
ans =

    1    2   12   13
    3    4   14   15
    5    6   16   17

>> [A,B]
ans =

    1    2   12   13
    3    4   14   15
    5    6   16   17

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值