# 机器学习 – octave

octave机器学习利器:

>> 5+6
ans =  11
>> 3-2
ans =  1
>> 5*8
ans =  40
>> 1/2
ans =  0.50000
>> 2^6
ans =  64
>> 1 == 2  %false
ans = 0
>> 1 ~= 2  %true
ans =  1
>> 8>1 && 0  %AND
ans = 0
>> 9>1 || 1  %OR
ans =  1
>> xor(1,0)
ans =  1
>> PS1('>>>');
>>>
>>>a = 3
a =  3
>>>a = 3;  #分号抑制打印
>>>
>>>a = 3.14;
>>>a
a =  3.1400
>>>disp(a);
3.1400
>>>disp(sprintf('2 decimals: %0.2f', a));
decimals: 3.14
>>>a=pi
a =  3.1416
>>>format long
>>>a
a =  3.14159265358979
>>>format short
>>>a
a =  3.1416
>>>A = [1 2; 3 4; 5 6]
A =
2
4
6

>>>a = [1 2;
4;
6]
a =
2
4
6
>>>v = [1 2 3]
v =
2   3

>>>v = [1; 2; 3]
v =
2
>>>v = 1:0.1:2
v =

Columns 1 through 4:

1.0000    1.1000    1.2000    1.3000

Columns 5 through 8:

1.4000    1.5000    1.6000    1.7000

Columns 9 through 11:

1.8000    1.9000    2.0000
>>>v = 1:6
v =

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

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

0.91025   0.82671   0.14067
0.90400   0.34350   0.51289
0.25501   0.24975   0.80750
// 高斯分布 (正态分布)
>>>w = randn(1,3)
w =

-0.052546  -1.786869   0.754202
w = -6 + sqrt(10)*(randn(1,10000));
hist(w)
hist(w, 50)
>> eye(4)
ans =

Diagonal Matrix
0   0   0
1   0   0
0   1   0
0   0   1
>> help

For help with individual commands and functions type

help NAME

(replace NAME with the name of the command or function you would

For a more detailed introduction to GNU Octave, please consult the
manual.  To read the manual from the prompt type

doc

GNU Octave is supported and developed by its user community.
>> A = [1 2; 3 4; 5 6]
A =
2
4
6

>> size(A)
ans =
2

>> sz = size(A)
sz =
2

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

>> length(V)
ans =  4
>> length(A)
ans =  3
>> pwd
ans = C:\Users\xin
>> cd 'E:\TEMPsrc\octave'
>> pwd
ans = E:\TEMPsrc\octave
>> ls
>> who
Variables in the current scope:

a    ans  b    c
>> whos
Variables in the current scope:

Attr Name        Size                     Bytes  Class

==== ====        ====                     =====  =====

a           1x1                          8  doubl
e
ans         1x17                        17  char
b           1x1                          8  doubl
e
c           1x1                          8  doubl
e
d           3x2                         48  doubl
e

Total is 26 elements using 89 bytes
>> who
Variables in the current scope:

a    ans  b    c    d

>> clear a
>> who
Variables in the current scope:

ans  b    c    d
>> save hello.mat d
>> clear
>> who
>> v = [1 2; 3 4; 5 6; 7 8; 9 0]
v =
2
4
6
8
0

< -ascii  %save as text(ASCII)
>> A = [1 2; 3 4; 5 6]
A =
2
4
6

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

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

1   2
5   6
>> A(:,2) = [10;11;12]
A =
10
11
12

>> A = [A, [100;101;102]]
A =
10   100
11   101
12   102
>> A(:)
ans =
3
10
12
101
>> A = [1 2; 3 4; 5 6];
>> B = [11 12; 13 14; 15 16];
>> C = [A B]
C =
2   11   12
4   13   14
6   15   16
>> C = [A; B]
C =
2
4
6
12
14
16
>> A = [1 2; 3 4; 5 6];
>> B = [11 12; 13 14; 15 16];
>> C = [1 1; 2 2];
>> A*C
ans =
5
11
17

>> A .* B
ans =
24
56
96

>> A .^ 2
ans =
4
16
36
>> V = [1; 2; 3];
>> 1 ./ V
ans =

1.00000
0.50000
0.33333

>> 1 ./ A
ans =

1.00000   0.50000
0.33333   0.25000
0.20000   0.16667
>> log(V)
ans =

0.00000
0.69315
1.09861

>> exp(V)
ans =

2.7183
7.3891
20.0855

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

>> v + ones(length(v), 1)
ans =
3

>> v + ones(3,1)
ans =
3

>> v + 1
ans =
3
>> A
A =
2
4
6

>> A'
ans =
3   5
4   6
>> a = [1 15 2 0.5]
a =

1.00000   15.00000    2.00000    0.50000

>> val = max(a)
val =  15
>> [val, ind] = max(a)
val =  15
ind =  2
a =

1.00000   15.00000    2.00000    0.50000

>> a < 3
ans =
0   1   1

>> find(a < 3)
ans =
3   4
>> A = magic(3)
A =
1   6
5   7
9   2

>> [r, c] = find(A >= 7)
r =
3

c =
2
>> a
a =

1.00000   15.00000    2.00000    0.50000

>> sum(a)
ans =  18.500
>> prod(a)
ans =  15
>> floor(a)
ans =
15    2    0

>> ceil(a)
ans =
15    2    1
>> max(rand(3), rand(3))
ans =

0.957477   0.083887   0.459507
0.799441   0.975439   0.927632
0.888604   0.942436   0.612661

>> A
A =
1   6
5   7
9   2

>> max(A, [], 1)
ans =
9   7
>> max(max(A))
ans =  9
>> max(A(:))
ans =  9
>> A = magic(5)
A =
24    1    8   15
5    7   14   16
6   13   20   22
12   19   21    3
18   25    2    9

>> sum(A,1)
ans =
65   65   65   65

>> sum(A,2)
ans =
65
65
>> sum(sum(A.*eye(5)))
ans =  65
>> eye(9)
ans =

Diagonal Matrix
0   0   0   0   0   0   0   0
1   0   0   0   0   0   0   0
0   1   0   0   0   0   0   0
0   0   1   0   0   0   0   0
0   0   0   1   0   0   0   0
0   0   0   0   1   0   0   0
0   0   0   0   0   1   0   0
0   0   0   0   0   0   1   0
0   0   0   0   0   0   0   1

>> flipud(eye(9))
ans =

Permutation Matrix
0   0   0   0   0   0   0   1
0   0   0   0   0   0   1   0
0   0   0   0   0   1   0   0
0   0   0   0   1   0   0   0
0   0   0   1   0   0   0   0
0   0   1   0   0   0   0   0
0   1   0   0   0   0   0   0
1   0   0   0   0   0   0   0
0   0   0   0   0   0   0   0
>> A = magic(3)
A =
1   6
5   7
9   2

>> pinv(A)
ans =

0.147222  -0.144444   0.063889
-0.061111   0.022222   0.105556
-0.019444   0.188889  -0.102778

>> temp = pinv(A)
temp =

0.147222  -0.144444   0.063889
-0.061111   0.022222   0.105556
-0.019444   0.188889  -0.102778

>> temp * A
ans =

1.00000   0.00000  -0.00000
-0.00000   1.00000   0.00000
0.00000   0.00000   1.00000
>> t=[0:0.01:0.98];
>> y1 = sin(2*pi*4*t);
>> plot(t,y1);
>> t=[0:0.01:0.98];
>> 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)
>> imagesc(A), colorbar, colormap gray;

Subscribe
Notify of