octave学习
移动数据
GNU Octave, version 4.4.0
Copyright (C) 2018 John W. Eaton and others.
This is free software; see the source code for copying conditions.
There is ABSOLUTELY NO WARRANTY; not even for MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. For details, type 'warranty'.
Octave was configured for "x86_64-w64-mingw32".
Additional information about Octave is available at https://www.octave.org.
Please contribute if you find this software useful.
For more information, visit https://www.octave.org/get-involved.html
Read https://www.octave.org/bugs.html to learn how to submit bug reports.
For information about changes from previous versions, type 'news'.
>> A
error: 'A' undefined near line 1 column 2
>> A = ]
parse error:
syntax error
>>> A = ]
^
>> A = [1 2; 3 4; 5 6]
A =
1 2
3 4
5 6
>> size(A)
ans =
3 2
>> size(A, 1)
ans = 3
>> pwd
ans = C:\Users\stone
>> cd 'C:\Users\stone'
>> pwd
ans = C:\Users\stone
>> ls
Volume in drive C has no label.
Volume Serial Number is C21D-94E3
Directory of C:\Users\stone
[.] [3D Objects]
[..] [Contacts]
[.anaconda] d
[.astropy] [Desktop]
[.atom] [Documents]
[.conda] [Downloads]
.condarc [Favorites]
[.config] [github]
.gitconfig java_error_in_pycharm_20868.log
[.IntelliJIdea2017.1] [Links]
[.keras] [Music]
[.m2] [OneDrive]
[.matplotlib] [Pictures]
.octave_hist [Saved Games]
[.Origin] [Searches]
[.PyCharm2017.1] [Videos]
[.QtWebEngineProcess]
5 File(s) 40,906 bytes
28 Dir(s) 37,169,750,016 bytes free
>> cd 'E:\python_workspace\ML'
>> pwd
ans = E:\python_workspace\ML
>> ls
Volume in drive E is 文文档档
Volume Serial Number is 000A-6B8A
Directory of E:\python_workspace\ML
[.] [..]
0 File(s) 0 bytes
2 Dir(s) 259,804,962,816 bytes free
>> load featuresX.dat
>> load priceY.dat
>> load(featuresX.dat)
error: matrix cannot be indexed with .
>> load('featuresX.dat')
>> who
Variables in the current scope:
A ans featuresX priceY
>> featuresX
featuresX =
2104 3
2103 2
2102 3
2011 7
2204 3
2304 5
2404 3
2504 4
2604 6
>> size(featuresX)
ans =
9 2
>> whos
Variables in the current scope:
Attr Name Size Bytes Class
==== ==== ==== ===== =====
A 3x2 48 double
ans 1x2 16 double
featuresX 9x2 144 double
priceY 9x1 72 double
Total is 35 elements using 280 bytes
>> v = priceY(1:10)
error: priceY(10): out of bound 9
>> v = priceY(1;10)
parse error:
syntax error
>>> v = priceY(1;10)
^
>> v = priceY(1:10)
error: priceY(10): out of bound 9
>> v = priceY(1:9)
v =
21043
21032
21023
20117
22043
23045
24043
25044
26046
>> save hello.mat v
>> save hello.mat v;
>> save hello.txt v;
>> save hello.txt v -ascii;
>> A = [1 2, 3 4, 5 6,]
A =
1 2 3 4 5 6
>> A = [1 2, 3 4, 5 6]
A =
1 2 3 4 5 6
>> A = [1 2; 3 4; 5 6]
A =
1 2
3 4
5 6
>> A(3,2)
ans = 6
>> A(2,:)
ans =
3 4
>> A(:.2)
parse error:
syntax error
>>> A(:.2)
^
>> A(:,2)
ans =
2
4
6
>> A([1 3], :)
ans =
1 2
5 6
>> A([1 3], :)
ans =
1 2
5 6
>> A(:,2) = [10;11;12]
A =
1 10
3 11
5 12
>> A = [A, [100, 101, 102]]
error: horizontal dimensions mismatch (3x2 vs 1x3)
>> A = [A, [100; 101; 102]]
A =
1 10 100
3 11 101
5 12 102
>> A(:)
ans =
1
3
5
10
11
12
100
101
102
>> A = [12;34;56]
A =
12
34
56
>> B =[11 12; 13 14; 15 16]
B =
11 12
13 14
15 16
>> C = [A B]
C =
12 11 12
34 13 14
56 15 16
>> A = [1 2;3 4;5 6]
A =
1 2
3 4
5 6
>> C = [A; B]
C =
1 2
3 4
5 6
11 12
13 14
15 16
计算数据
>> B =[11 12; 13 14; 15 16]
B =
11 12
13 14
15 16
>> A = [1 2;3 4;5 6]
A =
1 2
3 4
5 6
>> C = [11; 22]
C =
11
22
>> C = [1 1; 2 2]
C =
1 1
2 2
>> A * C
ans =
5 5
11 11
17 17
>> A * B
error: operator *: nonconformant arguments (op1 is 3x2, op2 is 3x2)
>> A . * B
parse error:
syntax error
>>> A . * B
^
>> A .* B
ans =
11 24
39 56
75 96
>> A .^ B
ans =
1 4096
1594323 268435456
30517578125 2821109907456
>> A = [1 2;3 4;5 6]
A =
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 =
0.000047522
0.000047547
0.000047567
0.000049709
0.000045366
0.000043393
0.000041592
0.000039930
0.000038394
>> V = [1; 2; 3 ]
V =
1
2
3
>> 1 ./ v
ans =
0.000047522
0.000047547
0.000047567
0.000049709
0.000045366
0.000043393
0.000041592
0.000039930
0.000038394
>> 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 =
Inf
Inf
Inf
Inf
Inf
Inf
Inf
Inf
Inf
>> exp(V)
ans =
2.7183
7.3891
20.0855
>> abs(V)
ans =
1
2
3
>> abs([-1; 2; 3])
ans =
1
2
3
>> -V
ans =
-1
-2
-3
>> V + one(length(V), 1)
error: 'one' undefined near line 1 column 5
>> V + ones(length(V), 1)
ans =
2
3
4
>> length(V)
ans = 3
>> ones(3,1 )
ans =
1
1
1
>> V + ones(3,1)
ans =
2
3
4
>> V + 1
ans =
2
3
4
>> A =
parse error:
syntax error
>>> A =
^
>> 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
>> val = max(a)
val = 15
>> [val, ind] = max(a)
val = 15
ind = 2
>> max(A)
ans =
5 6
>> A
A =
1 2
3 4
5 6
>> 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
>> help magic
'magic' is a function from the file D:\1b\octave\OCTAVE~1.0\share\octave\4.4.0\m\special-matrix\m
agic.m
-- magic (N)
Create an N-by-N magic square.
A magic square is an arrangement of the integers '1:n^2' such that
the row sums, column sums, and diagonal sums are all equal to the
same value.
Note: N must be a scalar greater than or equal to 3. If you supply
N less than 3, magic returns either a nonmagic square, or else the
degenerate magic squares 1 and [].
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.
>> [r,c] = find(A >= 7)
r =
1
3
2
c =
1
2
3
>> A
A =
8 1 6
3 5 7
4 9 2
>> A(2,3 )
ans = 7
>> help find
'find' is a built-in function from the file libinterp/corefcn/find.cc
-- IDX = find (X)
-- IDX = find (X, N)
-- IDX = find (X, N, DIRECTION)
-- [i, j] = find (...)
-- [i, j, v] = find (...)
Return a vector of indices of nonzero elements of a matrix, as a
row if X is a row vector or as a column otherwise.
To obtain a single index for each matrix element, Octave pretends
that the columns of a matrix form one long vector (like Fortran
arrays are stored). For example:
find (eye (2))
=> [ 1; 4 ]
If two inputs are given, N indicates the maximum number of elements
to find from the beginning of the matrix or vector.
If three inputs are given, DIRECTION should be one of "first" or
"last", requesting only the first or last N indices, respectively.
However, the indices are always returned in ascending order.
If two outputs are requested, 'find' returns the row and column
indices of nonzero elements of a matrix. For example:
[i, j] = find (2 * eye (2))
=> i = [ 1; 2 ]
=> j = [ 1; 2 ]
If three outputs are requested, 'find' also returns a vector
containing the nonzero values. For example:
[i, j, v] = find (3 * eye (2))
=> i = [ 1; 2 ]
=> j = [ 1; 2 ]
=> v = [ 3; 3 ]
Note that this function is particularly useful for sparse matrices,
as it extracts the nonzero elements as vectors, which can then be
used to create the original matrix. For example:
sz = size (a);
[i, j, v] = find (a);
b = sparse (i, j, v, sz(1), sz(2));
See also: nonzeros.
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.
>> a
a =
1.00000 15.00000 2.00000 0.50000
>> sum(a)
ans = 18.500
>> prod(a)
ans = 15
>> floor(a)
ans =
1 15 2 0
>> ceil(a)
ans =
1 15 2 1
>> type(a)
error: type: input arguments must be strings
error: called from
type at line 40 column 5
>> rand(3)
ans =
0.088875 0.949207 0.216275
0.591191 0.712044 0.918222
0.347278 0.510190 0.320720
>> max(rand(3), rand(3))
ans =
0.61998 0.60575 0.80974
0.73788 0.78479 0.82265
0.38734 0.34318 0.42776
>> A
A =
8 1 6
3 5 7
4 9 2
>> max(A, [], 1)
ans =
8 9 7
>> max(A, [], 2)
ans =
8
7
9
>> max(A)
ans =
8 9 7
>> max(max(A))
ans = 9
>> A(:)
ans =
8
3
4
1
5
9
6
7
2
>> max(A(:))
ans = 9
>> 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
>> sum(A, 1)
ans =
369 369 369 369 369 369 369 369 369
>> sum(A, 2)
ans =
369
369
369
369
369
369
369
369
369
>> 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
>> A
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
>> 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
>> sum(sum(A .*eye(9)))
ans = 369
>> sum(sum(A .*flipup(9)))
error: 'flipup' undefined near line 1 column 13
>> sum(sum(A .*flipud(9)))
ans = 29889
>> sum(sum(A .*flipud(eye(9))))
ans = 369
>> flipud(eye(9
flipud(eye(9)
parse error:
syntax error
>>> flipud(eye(9)
^
>> flipud(eye(9)
flipud(eye(9))
parse error:
syntax error
>>> flipud(eye(9))
^
>> 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
>> A = magic(3)
A =
8 1 6
3 5 7
4 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.0000e+000 3.3307e-016 -3.1086e-015
-6.0507e-015 1.0000e+000 6.3283e-015
3.0531e-015 1.1102e-016 1.0000e+000
>>
绘图数据
>> t = [0:0.01:0.98];
>> t
t =
Columns 1 through 9:
0.00000 0.01000 0.02000 0.03000 0.04000 0.05000 0.06000 0.07000 0.08000
Columns 10 through 18:
0.09000 0.10000 0.11000 0.12000 0.13000 0.14000 0.15000 0.16000 0.17000
Columns 19 through 27:
0.18000 0.19000 0.20000 0.21000 0.22000 0.23000 0.24000 0.25000 0.26000
Columns 28 through 36:
0.27000 0.28000 0.29000 0.30000 0.31000 0.32000 0.33000 0.34000 0.35000
Columns 37 through 45:
0.36000 0.37000 0.38000 0.39000 0.40000 0.41000 0.42000 0.43000 0.44000
Columns 46 through 54:
0.45000 0.46000 0.47000 0.48000 0.49000 0.50000 0.51000 0.52000 0.53000
Columns 55 through 63:
0.54000 0.55000 0.56000 0.57000 0.58000 0.59000 0.60000 0.61000 0.62000
Columns 64 through 72:
0.63000 0.64000 0.65000 0.66000 0.67000 0.68000 0.69000 0.70000 0.71000
Columns 73 through 81:
0.72000 0.73000 0.74000 0.75000 0.76000 0.77000 0.78000 0.79000 0.80000
Columns 82 through 90:
0.81000 0.82000 0.83000 0.84000 0.85000 0.86000 0.87000 0.88000 0.89000
Columns 91 through 99:
0.90000 0.91000 0.92000 0.93000 0.94000 0.95000 0.96000 0.97000 0.98000
>>
>>
>>
>> y1 = sin(2*pi*4*t);
>> plot(t, y1)
>> y2 = cos(2*pi*4*t);
>> plot(t, y2)
>> hold on ;
>> plot(t, y2, 'r')
>> xlable('time')
error: 'xlable' undefined near line 1 column 1
>> xlable('time')
error: 'xlable' undefined near line 1 column 1
>> plot(t, y2)
>> plot(t, y1)
>> hold on ;
>> plot(t, y2, 'r');
>> xlabel('time')
>> ylabel('value')
>> legend('sin', 'cos')
>> title('my plot')
>> cd 'E:\python_workspace\ML\print -dpng 'myplot.png'
parse error:
syntax error
>>> cd 'E:\python_workspace\ML\print -dpng 'myplot.png'
^
>> cd 'E:\python_workspace\ML' print -dpng 'myplot.png'
error: Invalid call to cd. Correct usage is:
-- cd DIR
-- cd
-- OLD_DIR = cd (DIR)
-- chdir ...
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.
>>
>> cd 'E:\python_workspace\ML'; 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])
>> help axis
'axis' is a function from the file D:\1b\octave\OCTAVE~1.0\share\octave\4.4.0\m\plot\appearance\axis.m
-- axis ()
-- axis ([X_LO X_HI])
-- axis ([X_LO X_HI Y_LO Y_HI])
-- axis ([X_LO X_HI Y_LO Y_HI Z_LO Z_HI])
-- axis ([X_LO X_HI Y_LO Y_HI Z_LO Z_HI C_LO C_HI])
-- axis (OPTION)
-- axis (OPTION1, OPTION2, ...)
-- axis (HAX, ...)
-- LIMITS = axis ()
Set axis limits and appearance.
The argument LIMITS should be a 2-, 4-, 6-, or 8-element vector.
The first and second elements specify the lower and upper limits
for the x-axis. The third and fourth specify the limits for the
y-axis, the fifth and sixth specify the limits for the z-axis, and
the seventh and eighth specify the limits for the color axis. The
special values -Inf and Inf may be used to indicate that the limit
should be automatically computed based on the data in the axes.
Without any arguments, 'axis' turns autoscaling on.
With one output argument, 'LIMITS = axis' returns the current axis
limits.
The vector argument specifying limits is optional, and additional
string arguments may be used to specify various axis properties.
The following options control the aspect ratio of the axes.
"square"
Force a square axis aspect ratio.
"equal"
Force x-axis unit distance to equal y-axis (and z-axis) unit
distance.
"normal"
Restore default aspect ratio.
The following options control the way axis limits are interpreted.
"auto"
"auto[xyz]"
Set the specified axes to have nice limits around the data or
all if no axes are specified.
"manual"
Fix the current axes limits.
"tight"
Fix axes to the limits of the data.
"image"
Equivalent to "tight" and "equal".
"vis3d"
Set aspect ratio modes to "manual" for rotation without
stretching.
The following options affect the appearance of tick marks.
"tic[xyz]"
Turn tick marks on for all axes, or turn them on for the
specified axes and off for the remainder.
"label[xyz]"
Turn tick labels on for all axes, or turn them on for the
specified axes and off for the remainder.
"nolabel"
Turn tick labels off for all axes.
Note: If there are no tick marks for an axes then there can be no
labels.
The following options affect the direction of increasing values on
the axes.
"xy"
Default y-axis, larger values are near the top.
"ij"
Reverse y-axis, smaller values are near the top.
The following options affects the visibility of the axes.
"on"
Make the axes visible.
"off"
Hide the axes.
If the first argument HAX is an axes handle, then operate on this
axes rather than the current axes returned by 'gca'.
Example 1: set X/Y limits and force a square aspect ratio
axis ([1, 2, 3, 4], "square");
Example 2: enable tick marks on all axes, enable tick mark labels
only on the y-axis
axis ("tic", "labely");
See also: xlim, ylim, zlim, caxis, daspect, pbaspect, box, grid.
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.
>> 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. color map gray
parse error:
syntax error
>>> imagesc(A).colorbar. color map gray
^
>> imagesc(A).colorbar. colormap gray
parse error:
syntax error
>>> imagesc(A).colorbar. colormap gray
^
>> imagesc(A).colorbar, colormap gray
error: indexing undefined value
>> imagesc(A), colorbar, colormap gray
>> A(1,2)
ans = 24
>> A(4,5)
ans = 3
>> imagesc(magic(15)), colorbar, colormap gray
>> a = 1
a = 1
>> a= 1. b =2 . c =3
parse error:
syntax error
>>> a= 1. b =2 . c =3
^
>> a= 1, b =2 , c =3
a = 1
b = 2
c = 3
>> a= 1; b =2 ; c =3
c = 3
>> a= 1; b =2 ; c =3;
>>
>>