目录
Control Statements: for, while, if Statements
Octave/Matlab Tutorial
Basic Operations
octave:1> 5+6
ans = 11
octave:2> 3-2
ans = 1
octave:3> 5*8
ans = 40
octave:4> 1/2
ans = 0.50000
octave:5> 2^6
ans = 64
octave:6>
octave:6> 1 == 2 % false
ans = 0
octave:7> 1 ~=2 % true
ans = 1
octave:8> 1 && 0 % AND
ans = 0
octave:9> 1 || 0 % OR
ans = 1
octave:10> xor(1,0)
ans = 1
octave:11>
octave:11>
octave:11> PS('>> ');
error: 'PS' undefined near line 1 column 1
octave:11> PS1('>> ');
>>
>> a = 3
a = 3
>> a = 3; % semicolon supressing output
>> b = 'hi';
>> b
b = hi
>> a
a = 3
>> c = (3>=1)
c = 1
>> c
c = 1
>> a = pi;
>> a
a = 3.1416
>> disp(a);
3.1416
>> disp(sprintf('2 decimals: %0.2f', a))
2 decimals: 3.14
>> disp(sprintf('6 decimals: %0.6f', a))
6 decimals: 3.141593
>> a
a = 3.1416
>> format long
>> a
a = 3.141592653589793
>> format short
>>
>>
>> A = [1,2; 3,4; 5,6]
A =
1 2
3 4
5 6
>> A = [1,2;]
A =
1 2
>> A = [1,2;
> 3,4;
> 5,6]
A =
1 2
3 4
5 6
>> row_vector = [1 2 3]
row_vector =
1 2 3
>> col_vector = [1;2;3]
col_vector =
1
2
3
>> v = 1: 0.1: 2
v =
1.0000 1.1000 1.2000 1.3000 1.4000 1.5000 1.6000 1.7000 1.8000 1.9000 2.0000
>> 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
>> w = ones(1,3)
w =
1 1 1
>> w = zeros(1,3)
w =
0 0 0
>> w = rand(1,3)
w =
0.067066 0.656105 0.981688
>> rand(3,3)
ans =
0.305543 0.031149 0.056141
0.158720 0.985101 0.179241
0.122642 0.709354 0.722481
>> rand(3,3)
ans =
0.64882 0.95000 0.86017
0.42894 0.31443 0.45231
0.91092 0.88357 0.93920
>> w = randn(1, 3)
w =
-1.30423 1.06293 -0.34975
>> w = randn(1,3) % a Gaussian distribution with mean zero and variance or standard deviation equal to one
w =
0.083350 -0.499730 0.582955
>> w = -6 + sqrt(10)*(randn(1,10000));
>> hist(w) % see picture Basic Operations01.gif
>> hist(w, 50) % see picture Basic Operations02.gif
>> I = eye(4)
I =
Diagonal Matrix
1 0 0 0
0 1 0 0
0 0 1 0
0 0 0 1
>> eye(3)
ans =
Diagonal Matrix
1 0 0
0 1 0
0 0 1
>>
>>
>> help eye
'eye' is a built-in function from the file libinterp/corefcn/data.cc
-- eye (N)
-- eye (M, N)
-- eye ([M N])
-- eye (..., CLASS)
Return an identity matrix.
If invoked with a single scalar argument N, return a square NxN
identity matrix.
If supplied two scalar arguments (M, N), 'eye' takes them to be the
number of rows and columns. If given a vector with two elements,
'eye' uses the values of the elements as the number of rows and
columns, respectively. For example:
eye (3)
=> 1 0 0
0 1 0
0 0 1
The following expressions all produce the same result:
eye (2)
==
eye (2, 2)
==
eye (size ([1, 2; 3, 4]))
The optional argument CLASS, allows 'eye' to return an array of the
specified type, like
val = zeros (n,m, "uint8")
Calling 'eye' with no arguments is equivalent to calling it with an
argument of 1. Any negative dimensions are treated as zero. These
odd definitions are for compatibility with MATLAB.
See also: speye, ones, zeros.
Additional help for built-in functions and operators is
available in the online version of the manual. Use the command
'doc <topic>' to search the manual index.
Help and information about Octave is also available on the WWW
at https://www.octave.org and via the help@octave.org
mailing list.
>> help rand
'rand' is a built-in function from the file libinterp/corefcn/rand.cc
-- rand (N)
-- rand (M, N, ...)
-- rand ([M N ...])
-- V = rand ("state")
-- rand ("state", V)
-- rand ("state", "reset")
-- V = rand ("seed")
-- rand ("seed", V)
-- rand ("seed", "reset")
-- rand (..., "single")
-- rand (..., "double")
Return a matrix with random elements uniformly distributed on the
interval (0, 1).
The arguments are handled the same as the arguments for 'eye'.
You can query the state of the random number generator using the
form
v = rand ("state")
This returns a column vector V of length 625. Later, you can
restore the random number generator to the state V using the form
rand ("state", v)
You may also initialize the state vector from an arbitrary vector
of length <= 625 for V. This new state will be a hash based on the
value of V, not V itself.
By default, the generator is initialized from '/dev/urandom' if it
is available, otherwise from CPU time, wall clock time, and the
current fraction of a second. Note that this differs from MATLAB,
which always initializes the state to the same state at startup.
To obtain behavior comparable to MATLAB, initialize with a
deterministic state vector in Octave's startup files (*note Startup
Files::).
To compute the pseudo-random sequence, 'rand' uses the Mersenne
Twister with a period of 2^{19937}-1 (See M. Matsumoto and T.
Nishimura, 'Mersenne Twister: A 623-dimensionally equidistributed
uniform pseudorandom number generator', ACM Trans. on Modeling and
Computer Simulation Vol. 8, No. 1, pp. 3-30, January 1998,
<http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/emt.html>). Do
*not* use for cryptography without securely hashing several
returned values together, otherwise the generator state can be
learned after reading 624 consecutive values.
Older versions of Octave used a different random number generator.
The new generator is used by default as it is significantly faster
than the old generator, and produces random numbers with a
significantly longer cycle time. However, in some circumstances it
might be desirable to obtain the same random sequences as produced
by the old generators. To do this the keyword "seed" is used to
specify that the old generators should be used, as in
rand ("seed", val)
which sets the seed of the generator to VAL. The seed of the
generator can be queried with
s = rand ("seed")
However, it should be noted that querying the seed will not cause
'rand' to use the old generators, only setting the seed will. To
cause 'rand' to once again use the new generators, the keyword
"state" should be used to reset the state of the 'rand'.
The state or seed of the generator can be reset to a new random
value using the "reset" keyword.
The class of the value returned can be controlled by a trailing
"double" or "single" argument. These are the only valid classes.
See also: randn, rande, randg, randp.
Additional help for built-in functions and operators is
available in the online version of the manual. Use the command
'doc <topic>' to search the manual index.
Help and information about Octave is also available on the WWW
at https://www.octave.org and via the help@octave.org
mailing list.
>> help help
'help' is a function from the file D:\Octave\Octave-5.1.0.0\mingw64\share\octave\5.1.0\m\help\help.m
-- help NAME
-- help --list
-- help .
-- help
Display the help text for NAME.
For example, the command 'help help' prints a short message
describing the 'help' command.
Given the single argument '--list', list all operators, keywords,
built-in functions, and loadable functions available in the current
session of Octave.
Given the single argument '.', list all operators available in the
current session of Octave.
If invoked without any arguments, 'help' displays instructions on
how to access help from the command line.
The help command can provide information about most operators, but
NAME must be enclosed by single or double quotes to prevent the
Octave interpreter from acting on NAME. For example, 'help "+"'
displays help on the addition operator.
See also: doc, lookfor, which, info.
Additional help for built-in functions and operators is
available in the online version of the manual. Use the command
'doc <topic>' to search the manual index.
Help and information about Octave is also available on the WWW
at https://www.octave.org and via the help@octave.org
mailing list.
>>
Basic Operations01.gif
Basic Operations02.gif
Moving Data Around
octave:1> A = [1 2; 3 4; 5 6]
A =
1 2
3 4
5 6
octave:2> size(A)
ans =
3 2
octave:3> sz = size(A)
sz =
3 2
octave:4> size(sz)
ans =
1 2
octave:6> size(A, 2) % #columns
ans = 2
octave:7> size(A, 1) % #rows
ans = 3
octave:8> v = [1 2 3 4]
v =
1 2 3 4
octave:9> length(v)
ans = 4
octave:10> length(A)
ans = 3
octave:11> A = transpose(A)
A =
1 3 5
2 4 6
octave:12> length(A)
ans = 3
octave:13>
octave:1> load ex1data1.txt
error: load: unable to find file ex1data1.txt
octave:1> pwd
ans = C:\Users\MoMo
octave:2> cd 'C:\Users\MoMo\Desktop'
octave:3> ls
驱动器 C 中的卷没有标签。
卷的序列号是 CE48-5CF4
C:\Users\MoMo\Desktop 的目录
[.] 01.png Excel 2013.lnk
[..] 1.JPG PowerPoint 2013.lnk
!gra - 快捷方式.lnk computer.lnk [tmp]
!培养方案 - 快捷方式.lnk ex1data1.txt Word 2013.lnk
9 个文件 371,058 字节
3 个目录 30,417,072,128 可用字节
octave:4> load ex1data1.txt
octave:5> load('ex1data1.txt') % the same as the previous instruction
octave:6> who
Variables in the current scope:
ans ex1data1
octave:7> ex1data1
ex1data1 =
6.11010 17.59200
5.52770 9.13020
8.51860 13.66200
7.00320 11.85400
5.85980 6.82330
8.38290 11.88600
7.47640 4.34830
8.57810 12.00000
6.48620 6.59870
5.05460 3.81660
5.71070 3.25220
14.16400 15.50500
5.73400 3.15510
8.40840 7.22580
5.64070 0.71618
5.37940 3.51290
6.36540 5.30480
5.13010 0.56077
6.42960 3.65180
7.07080 5.38930
6.18910 3.13860
20.27000 21.76700
5.49010 4.26300
6.32610 5.18750
5.56490 3.08250
18.94500 22.63800
12.82800 13.50100
10.95700 7.04670
13.17600 14.69200
22.20300 24.14700
5.25240 -1.22000
6.58940 5.99660
9.24820 12.13400
5.89180 1.84950
8.21110 6.54260
7.93340 4.56230
8.09590 4.11640
5.60630 3.39280
12.83600 10.11700
6.35340 5.49740
5.40690 0.55657
6.88250 3.91150
11.70800 5.38540
5.77370 2.44060
7.82470 6.73180
7.09310 1.04630
5.07020 5.13370
5.80140 1.84400
11.70000 8.00430
5.54160 1.01790
7.54020 6.75040
5.30770 1.83960
7.42390 4.28850
7.60310 4.99810
6.33280 1.42330
6.35890 -1.42110
6.27420 2.47560
5.63970 4.60420
9.31020 3.96240
9.45360 5.41410
8.82540 5.16940
5.17930 -0.74279
21.27900 17.92900
14.90800 12.05400
18.95900 17.05400
7.21820 4.88520
8.29510 5.74420
10.23600 7.77540
5.49940 1.01730
20.34100 20.99200
10.13600 6.67990
7.33450 4.02590
6.00620 1.27840
7.22590 3.34110
5.02690 -2.68070
6.54790 0.29678
7.53860 3.88450
5.03650 5.70140
10.27400 6.75260
5.10770 2.05760
5.72920 0.47953
5.18840 0.20421
6.35570 0.67861
9.76870 7.54350
6.51590 5.34360
8.51720 4.24150
9.18020 6.79810
6.00200 0.92695
5.52040 0.15200
5.05940 2.82140
5.70770 1.84510
7.63660 4.29590
5.87070 7.20290
5.30540 1.98690
8.29340 0.14454
13.39400 9.05510
5.43690 0.61705
octave:8> ans
ans = C:\Users\MoMo
octave:9> size(ex1data1)
ans =
97 2
octave:10> ans
ans =
97 2
octave:11> whos
Variables in the current scope:
Attr Name Size Bytes Class
==== ==== ==== ===== =====
ans 1x2 16 double
ex1data1 97x2 1552 double
Total is 196 elements using 1568 bytes
octave:12> D = ex1data1(1:10)
D =
6.1101 5.5277 8.5186 7.0032 5.8598 8.3829 7.4764 8.5781 6.4862 5.0546
octave:13> ex1data1(1:20)
ans =
Columns 1 through 11:
6.1101 5.5277 8.5186 7.0032 5.8598 8.3829 7.4764 8.5781 6.4862 5.0546 5.7107
Columns 12 through 20:
14.1640 5.7340 8.4084 5.6407 5.3794 6.3654 5.1301 6.4296 7.0708
octave:14> v = ex1data1(1:5)
v =
6.1101 5.5277 8.5186 7.0032 5.8598
octave:15> save hello.mat v;
octave:16> ls
驱动器 C 中的卷没有标签。
卷的序列号是 CE48-5CF4
C:\Users\MoMo\Desktop 的目录
[.] 01.png Excel 2013.lnk Word 2013.lnk
[..] 1.JPG hello.mat
!gra - 快捷方式.lnk computer.lnk PowerPoint 2013.lnk
!培养方案 - 快捷方式.lnk ex1data1.txt [tmp]
10 个文件 371,294 字节
3 个目录 30,416,936,960 可用字节
octave:17> load hello.mat
octave:18> who
Variables in the current scope:
D ans ex1data1 v
octave:19> clear ex1data1
octave:20> who
Variables in the current scope:
D ans v
octave:21> clear
octave:22> who
octave:23> v
error: 'v' undefined near line 1 column 1
octave:23> load hello.mat
octave:24> who
Variables in the current scope:
v
octave:25> v
v =
6.1101 5.5277 8.5186 7.0032 5.8598
octave:26> save hello.mat v; % save the data in v into hello.mat in binary format
octave:27> save hello.txt v -ascii; % save the data in v into hello.txt in human readable format
octave:28> ls
驱动器 C 中的卷没有标签。
卷的序列号是 CE48-5CF4
C:\Users\MoMo\Desktop 的目录
[.] 01.png Excel 2013.lnk PowerPoint 2013.lnk
[..] 1.JPG hello.dat [tmp]
!gra - 快捷方式.lnk computer.lnk hello.mat Word 2013.lnk
!培养方案 - 快捷方式.lnk ex1data1.txt hello.txt
12 个文件 371,607 字节
3 个目录 30,413,930,496 可用字节
octave:29>
octave:1> A = [1 2; 3 4; 5 6]
A =
1 2
3 4
5 6
octave:2> A(3, 2)
ans = 6
octave:3> A(3, 1)
ans = 5
octave:4> A(2, :) % ':' means every element along that row/column
ans =
3 4
octave:5> A(:, 1)
ans =
1
3
5
octave:6> A([1 3], :)
ans =
1 2
5 6
octave:7> A(:, [1 2])
ans =
1 2
3 4
5 6
octave:8> A(:, 2) = [10; 11; 12]
A =
1 10
3 11
5 12
octave:9> A = [A, [100; 101; 102]]; % append another column vector to right side
octave:10> A
A =
1 10 100
3 11 101
5 12 102
octave:12> A(:) % put all elements of A into a single vector
ans =
1
3
5
10
11
12
100
101
102
octave:13>
octave:13>
octave:13>
octave:13> A = [1 2; 3 4; 5 6]
A =
1 2
3 4
5 6
octave:14> B = [11 12; 13 14; 15 16]
B =
11 12
13 14
15 16
octave:15> C = [A B] % concatenating two matrices
C =
1 2 11 12
3 4 13 14
5 6 15 16
octave:16> D = [A; B] % concatenating two matrices
D =
1 2
3 4
5 6
11 12
13 14
15 16
octave:17> size(C)
ans =
3 4
octave:18> size(D)
ans =
6 2
octave:19> [A B]
ans =
1 2 11 12
3 4 13 14
5 6 15 16
octave:20> [A, B]
ans =
1 2 11 12
3 4 13 14
5 6 15 16
octave:21> [A; B]
ans =
1 2
3 4
5 6
11 12
13 14
15 16
octave:22>
Computing On Data
octave:1> A = [1 2; 3 4; 5 6]
A =
1 2
3 4
5 6
octave:2> B = [11 12; 13 14; 15 16]
B =
11 12
13 14
15 16
octave:3> C = [1 1; 2 2]
C =
1 1
2 2
octave:4> A*C
ans =
5 5
11 11
17 17
octave:6> A
A =
1 2
3 4
5 6
octave:7> B
B =
11 12
13 14
15 16
octave:8> A .* B % per element
ans =
11 24
39 56
75 96
octave:10> A
A =
1 2
3 4
5 6
octave:11> A .^ 2 % per element
ans =
1 4
9 16
25 36
octave:12> v = [1; 2; 3]
v =
1
2
3
octave:13> 1 ./ v % per element (inverse)
ans =
1.00000
0.50000
0.33333
octave:14> 1 ./ A
ans =
1.00000 0.50000
0.33333 0.25000
0.20000 0.16667
octave:15> log(v) % element wise log
ans =
0.00000
0.69315
1.09861
octave:16> exp(v) % element wise ln
ans =
2.7183
7.3891
20.0855
octave:17> v = [1; -2; 3]
v =
1
-2
3
octave:18> abs(v)
ans =
1
2
3
octave:19> -v
ans =
-1
2
-3
octave:20> -1 * v
ans =
-1
2
-3
octave:21> v + ones(length(v), 1)
ans =
2
-1
4
octave:22> length(v)
ans = 3
octave:23> ones(3, 1)
ans =
1
1
1
octave:24> v + ones(3,1)
ans =
2
-1
4
octave:25> v + 1
ans =
2
-1
4
octave:26>
octave:26>
octave:26> A
A =
1 2
3 4
5 6
octave:27> A'
ans =
1 3 5
2 4 6
octave:28> (A')'
ans =
1 2
3 4
5 6
octave:29>
octave:29>
octave:29> a = [1 15 2 0.5]
a =
1.00000 15.00000 2.00000 0.50000
octave:30> val = max(a)
val = 15
octave:31> [val, ind] = max(a) % get the value and the index
val = 15
ind = 2
octave:32> max(A) % column wise maximum
ans =
5 6
octave:33>
octave:33>
octave:33> a
a =
1.00000 15.00000 2.00000 0.50000
octave:34> a < 3 % per element
ans =
1 0 1 1
octave:35> find(a < 3)
ans =
1 3 4
octave:36> A = magic(3) % all the rows, columns, and diagonals sum up to the same thing
A =
8 1 6
3 5 7
4 9 2
octave:37> [r,c] = find(A >= 7)
r =
1
3
2
c =
1
2
3
octave:39> A(1,1) >= 7
ans = 1
octave:40> A(3,2) >= 7
ans = 1
octave:41> A(2,3) >= 7
ans = 1
octave:42>
octave:42>
octave:42> a
a =
1.00000 15.00000 2.00000 0.50000
octave:43> sum(a)
ans = 18.500
octave:44> prod(a) % product
ans = 15
octave:45> floor(a)
ans =
1 15 2 0
octave:46> ceil(a)
ans =
1 15 2 1
octave:47>
octave:47> rand(3)
ans =
0.451161 0.912815 0.539989
0.048420 0.343431 0.713041
0.393327 0.082036 0.535049
octave:48> max(rand(3), rand(3)) % element wise maximum of two random 3*3 matrices
ans =
0.78999 0.48031 0.97840
0.44580 0.84540 0.50117
0.63424 0.66027 0.96962
octave:49>
octave:49>
octave:49> A
A =
8 1 6
3 5 7
4 9 2
octave:50> max(A,[],1) % column wise maximum
ans =
8 9 7
octave:51> max(A, [], 2) % row wise maximum
ans =
8
7
9
octave:52> max(A) % default of max(A,[], 1)
ans =
8 9 7
octave:53> A(:)
ans =
8
3
4
1
5
9
6
7
2
octave:54> max(A(:))
ans = 9
octave:56> max(max(A))
ans = 9
octave:57>
octave:57>
octave:57> A = magic(9)
A =
47 58 69 80 1 12 23 34 45
57 68 79 9 11 22 33 44 46
67 78 8 10 21 32 43 54 56
77 7 18 20 31 42 53 55 66
6 17 19 30 41 52 63 65 76
16 27 29 40 51 62 64 75 5
26 28 39 50 61 72 74 4 15
36 38 49 60 71 73 3 14 25
37 48 59 70 81 2 13 24 35
octave:58> sum(A, 1) % per column sum
ans =
369 369 369 369 369 369 369 369 369
octave:59> sum(A, 2) % per row sum
ans =
369
369
369
369
369
369
369
369
369
octave:60> eye(9)
ans =
Diagonal Matrix
1 0 0 0 0 0 0 0 0
0 1 0 0 0 0 0 0 0
0 0 1 0 0 0 0 0 0
0 0 0 1 0 0 0 0 0
0 0 0 0 1 0 0 0 0
0 0 0 0 0 1 0 0 0
0 0 0 0 0 0 1 0 0
0 0 0 0 0 0 0 1 0
0 0 0 0 0 0 0 0 1
octave:61> A .* eye(9)
ans =
47 0 0 0 0 0 0 0 0
0 68 0 0 0 0 0 0 0
0 0 8 0 0 0 0 0 0
0 0 0 20 0 0 0 0 0
0 0 0 0 41 0 0 0 0
0 0 0 0 0 62 0 0 0
0 0 0 0 0 0 74 0 0
0 0 0 0 0 0 0 14 0
0 0 0 0 0 0 0 0 35
octave:63> sum(A .* eye(9))
ans =
47 68 8 20 41 62 74 14 35
octave:64> sum( sum(A .* eye(9)) )
ans = 369
octave:65> flipud(eye(9))
ans =
Permutation Matrix
0 0 0 0 0 0 0 0 1
0 0 0 0 0 0 0 1 0
0 0 0 0 0 0 1 0 0
0 0 0 0 0 1 0 0 0
0 0 0 0 1 0 0 0 0
0 0 0 1 0 0 0 0 0
0 0 1 0 0 0 0 0 0
0 1 0 0 0 0 0 0 0
1 0 0 0 0 0 0 0 0
octave:66> A .* flipud(eye(9))
ans =
0 0 0 0 0 0 0 0 45
0 0 0 0 0 0 0 44 0
0 0 0 0 0 0 43 0 0
0 0 0 0 0 42 0 0 0
0 0 0 0 41 0 0 0 0
0 0 0 40 0 0 0 0 0
0 0 39 0 0 0 0 0 0
0 38 0 0 0 0 0 0 0
37 0 0 0 0 0 0 0 0
octave:67> sum( sum(A .* flipud(eye(9)) ))
ans = 369
octave:68>
octave:68>
octave:68>
octave:68> A = magic(3)
A =
8 1 6
3 5 7
4 9 2
octave:69> pinv(A) % inverse of A
ans =
0.147222 -0.144444 0.063889
-0.061111 0.022222 0.105556
-0.019444 0.188889 -0.102778
octave:70> tmp = pinv(A)
tmp =
0.147222 -0.144444 0.063889
-0.061111 0.022222 0.105556
-0.019444 0.188889 -0.102778
octave:71> tmp * A
ans =
1.0000e+00 2.0817e-16 -3.1641e-15
-6.1062e-15 1.0000e+00 6.2450e-15
3.0531e-15 4.1633e-17 1.0000e+00
octave:72>
Plotting Data
octave:1> t = [0: 0.01: 0.98];
octave:2> y1 = sin(2*pi*4*t);
octave:3> plot(t, y1); % see Plotting Data01.gif
octave:4> y2 = cos(2*pi*4*t);
octave:5> plot(t, y2); % see Plotting Data02.gif
octave:6> % plot two graphs together
octave:6> plot(t, y1);
octave:7> hold on;
octave:8> plot(t, y2); % default to color red
octave:9> plot(t, y2, 'r'); % specify color red % see Plotting Data03.gif
octave:10> xlabel('time')
octave:11> ylabel('value')
octave:12> legend('sin', 'cos')
octave:13> title('my plot')
octave:16> cd 'C:\Users\MoMo\Desktop'
octave:17> pwd
ans = C:\Users\MoMo\Desktop
octave:18> print dpng 'myPlot.png' % save the picture % see myPlot.png
octave:19> ls
驱动器 C 中的卷没有标签。
卷的序列号是 CE48-5CF4
C:\Users\MoMo\Desktop 的目录
[.] !培养方案 - 快捷方式.lnk Excel 2013.lnk [tmp]
[..] 1.JPG myPlot.png Word 2013.lnk
!gra - 快捷方式.lnk computer.lnk PowerPoint 2013.lnk
8 个文件 94,028 字节
3 个目录 30,315,008,000 可用字节
octave:20> close % close the picture
octave:21>
octave:21>
octave:21> figure(1); plot(t,y1);
octave:22> figure(2); plot(t, y2); % see 捕获01.jpg
octave:26> close
octave:27> close
octave:28> subplot(1, 2, 1); % divides plot into a 1*2 grid, access the first element % see Plotting Data04.gif
octave:31> plot(t, y1); % see Plotting Data05.gif
octave:32> subplot(1, 2, 2); % divides plot into a 1*2 grid, access the second element % see Plotting Data06.gif
octave:33> plot(t, y2); % see Plotting Data07.gif
octave:34> close
octave:35> plot(t, y1);
octave:36> axis([0.5 1 -1 1]) % see Plotting Data08.gif
octave:37> clf; % clear the figure
octave:38> 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
octave:39> imagesc(A) % see Plotting Data09.gif
octave:40> imagesc(A), colorbar, colormap gray; % see Plotting Data10.gif
octave:41> imagesc(magic(15)), colorbar, colormap gray; % see Plotting Data11.gif
octave:42>
octave:42> a = 1, b = 2, c = 3
a = 1
b = 2
c = 3
octave:43>
Plotting Data01.gif
Plotting Data02.gif
Plotting Data03.gif
myPlot.png
捕获01.jpg
Plotting Data04.gif
Plotting Data05.gif
Plotting Data06.gif
Plotting Data07.gif
Plotting Data08.gif
Plotting Data09.gif
Plotting Data10.gif
Plotting Data11.gif
Control Statements: for, while, if Statements
octave:1> PS1('>> ')
>> v = zeros(10,1)
v =
0
0
0
0
0
0
0
0
0
0
>> for i=1:10,
> v(i) = 2^i;
> end;
>> v
v =
2
4
8
16
32
64
128
256
512
1024
>> indices = 1:10;
>> indices
indices =
1 2 3 4 5 6 7 8 9 10
>> for i=indices,
> disp(i);
> end;
1
2
3
4
5
6
7
8
9
10
>> v
v =
2
4
8
16
32
64
128
256
512
1024
>> i = 1;
>> while i <= 5,
> v(i) = 100;
> i = i+1;
> end;
>> v
v =
100
100
100
100
100
64
128
256
512
1024
>> % break, continue can be used in octave
>> 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)
ans = 999
>> v(1) = 2;
>> if v(1) == 1.
> disp('The value is one');
> elseif v(1) == 2,
> disp('The value is two');
> else
> disp('The value is not one or two');
> end;
The value is two
>>
>>
>> % quit / exit will close the Octave CLI
>>
>>
>> pwd
ans = C:\Users\MoMo\Desktop
>> ls
驱动器 C 中的卷没有标签。
卷的序列号是 CE48-5CF4
C:\Users\MoMo\Desktop 的目录
[.] 1.JPG squareAndCubeThisNumber.m
[..] computer.lnk squareThisNumber.m
!gra - 快捷方式.lnk Excel 2013.lnk [tmp]
!培养方案 - 快捷方式.lnk PowerPoint 2013.lnk Word 2013.lnk
9 个文件 23,351 字节
3 个目录 30,410,297,344 可用字节
>>
>>
>> cd 'C:\Users\MoMo'
>> squareThisNumber(5);
error: 'squareThisNumber' undefined near line 2 column 1
>> pwd
ans = C:\Users\MoMo
>> cd 'C:\Users\MoMo\Desktop'
>> squareThisNumber(5)
ans = 25
>>
>>
>> % Octave Search Path (advanced/optional)
>> addpath('C:\Users\MoMo\Desktop')
>> cd 'C:\'
>> pwd
ans = C:\
>> squareThisNumber(5)
ans = 25
>>
>>
>> [a,b] = squareAndCubeThisNumber(5)
a = 25
b = 125
>> squareAndCubeThisNumber(5) % wrong
ans = 25
>>
>>
>> % see CostFunction.jpg
>> cd 'C:\Users\MoMo\Desktop'
>> pwd
ans = C:\Users\MoMo\Desktop
>> ls
驱动器 C 中的卷没有标签。
卷的序列号是 CE48-5CF4
C:\Users\MoMo\Desktop 的目录
[.] 1.JPG PowerPoint 2013.lnk Word 2013.lnk
[..] computer.lnk squareAndCubeThisNumber.m
!gra - 快捷方式.lnk costFunctionJ.m squareThisNumber.m
!培养方案 - 快捷方式.lnk Excel 2013.lnk [tmp]
10 个文件 23,676 字节
3 个目录 30,407,446,528 可用字节
>> X = [1 1; 1 2; 1 3] % design matrix of the example
X =
1 1
1 2
1 3
>> y = [1; 2; 3]
y =
1
2
3
>> theta = [0;1]
theta =
0
1
>> size(X,1) % 1 -- row
ans = 3
>> size(X,2) % 2 -- column
ans = 2
>> X*theta
ans =
1
2
3
>> (X*theta-y) .^ 2
ans =
0
0
0
>> j = costFunctionJ(X, y, theta)
j = 0
>> theta = [0; 0]
theta =
0
0
>> (1^2 + 2^2 + 3^2) / (2*3)
ans = 2.3333
>> j = costFunctionJ(X, y, theta)
j = 2.3333
>>
% squareThisNumber.m
function y = squareThisNumber(x)
y = x^2;
% squareAndCubeThisNumber.m
function [y1,y2] = squareAndCubeThisNumber(x)
y1 = x^2;
y2 = x^3;
% costFunctionJ.m
function J = costFunctionJ(X, y, theta)
% X is the "design matrix" containing our training examples.
% y is the class labels
m = size(X,1); % #training examples
predictions = X*theta; % predictions of hypothesis on all m examples
sqrErrors = (predictions-y) .^ 2; % squared errors
J = 1/(2*m) * sum(sqrErrors);
CostFunction.jpg