第2周--Basic Operations
>> 1 && 0
ans = 0
>> 1 || 0
ans = 1
>> xor(1,0)
ans = 1
>> PS('>> ');
error: 'PS' undefined near line 1 column 1
>> PS1('>> '); %PS1() is a function that is used for Query or set the primary prompt string.
>> PS1('>> ')
>> T = PS1('>>')
T = >>
>>clear
>>a = 3
a = 3
>>b = 'hi';
>>b
b = hi
>>c = (3>=1);
>>c
c = 1
>>a = pi;
>>a
a = 3.1416
>>disp(a);
3.1416
>>disp(sprintf('2 decimals: %0.2f',a)) %Here to control its bits after the point are 2
2 decimals: 3.14
>>disp(sprintf('6 decimals: %0.6f',a)) %Here to control its bits after the point are 6
6 decimals: 3.141593
>>format long
>>a
a = 3.141592653589793
>>format short
>>a
a = 3.1416
接下来这部分是基本的一些矩阵相关
>>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
>>
>>V = [1 2 3]
V =
1 2 3
>>V = [1; 2; 3]
V =
1
2
3
>>V = 1:0.1:2
V =
Columns 1 through 9:
1.0000 1.1000 1.2000 1.3000 1.4000 1.5000 1.6000 1.7000 1.8000
Columns 10 and 11:
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
>>c = [2 2 2; 2 2 2]
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.56964 0.58711 0.16050
>>rand(3,3)
ans =
0.31862 0.17487 0.89735
0.33011 0.81878 0.42345
0.61995 0.61151 0.66674
>>
>>rand(3,3)
ans =
0.55248 0.41297 0.69621
0.76598 0.86068 0.19670
0.18062 0.59087 0.85608
>>w = randn(1,3)
w =
-0.55813 -0.16527 -1.17928
>>
>>
>>
>>w = -6 + sqrt(10) * (randn(1,10000));
>>>w = -6 + sqrt(10) * (randn(1,10000))
parse error:
syntax error
>>> >w = -6 + sqrt(10) * (randn(1,10000))
^
>>hist(w)
>>hist(w)
>>hist(w,50)
>>
>>
>>eye(4)
ans =
Diagonal Matrix
1 0 0 0
0 1 0 0
0 0 1 0
0 0 0 1
>>I = eye(4)
I =
Diagonal Matrix
1 0 0 0
0 1 0 0
0 0 1 0
0 0 0 1
>>I = eye(6)
I =
Diagonal Matrix
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
>>
>>
>>help
For help with individual commands and functions type
help NAME
(replace NAME with the name of the command or function you would
like to learn more about; for an operator, enclose "NAME" in quotes).
For a more detailed introduction to GNU Octave, consult the manual.
The manual may be read from the prompt by typing
doc
GNU Octave is supported and developed by its user community.
For more information visit https://www.octave.org.
>>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.
第2周--Moving data around
ꦁꃿ>> 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(A,1)
ans = 3
>> size(A,2)
ans = 2
>> V = [1 2 3 4]
V =
1 2 3 4
>> length(v)
error: 'v' undefined near line 1 column 8
>> length(V)
ans = 4
>> length(A)
ans = 3
>>
>> PWD
error: 'PWD' undefined near line 1 column 1
>> pwd
ans = C:\Users\Administrator
>> cd 'C:\Users\Desktop
parse error:
syntax error
>>> cd 'C:\Users\Desktop
^
>> who
Variables in the current scope:
A V ans sz
>> whos
Variables in the current scope:
Attr Name Size Bytes Class
==== ==== ==== ===== =====
A 3x2 48 double
V 1x4 32 double
ans 1x22 22 char
sz 1x2 16 double
Total is 34 elements using 118 bytes
>> V
V =
1 2 3 4
>> save hello.mat V
>> load heelo.mat
error: load: unable to find file heelo.mat
>> load hello.mat
>> who
Variables in the current scope:
A V ans sz
>> whos
Variables in the current scope:
Attr Name Size Bytes Class
==== ==== ==== ===== =====
A 3x2 48 double
V 1x4 32 double
ans 1x22 22 char
sz 1x2 16 double
Total is 34 elements using 118 bytes
>> clear V
>> whos
Variables in the current scope:
Attr Name Size Bytes Class
==== ==== ==== ===== =====
A 3x2 48 double
ans 1x22 22 char
sz 1x2 16 double
Total is 30 elements using 86 bytes
>> load hello.mat
>> whos
Variables in the current scope:
Attr Name Size Bytes Class
==== ==== ==== ===== =====
A 3x2 48 double
V 1x4 32 double
ans 1x22 22 char
sz 1x2 16 double
Total is 34 elements using 118 bytes
>> save hello.txt V -ascii %save as text (ASCII)
>> save hello1.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,:)% ":" means every element along the row/column
ans =
3 4
>> A(:,3)
error: A(_,3): but A has size 3x2
>> A(:,2)
ans =
2
4
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]]%Append another column vector to the right
A =
1 10 100
3 11 101
5 12 102
>> size(A)
ans =
3 3
>> A(:)%put all the elements of A into a single vecotr
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 = [11 12; 13 14; 15 16]
B =
11 12
13 14
15 16
>> C = [A B]
C =
1 2 11 12
3 4 13 14
5 6 15 16
>> C = [A; B]
C =
1 2
3 4
5 6
11 12
13 14
15 16
>> size(C)
ans =
6 2
第2周--Computing on data
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'.
>> 2 * cos(pi)
ans = -2
>> 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(A,1)
ans = 3
>> size(A,2)
ans = 2
>> V = [1 2 3 4]
V =
1 2 3 4
>> length(v)
error: 'v' undefined near line 1 column 8
>> length(V)
ans = 4
>> length(A)
ans = 3
>>
>> PWD
error: 'PWD' undefined near line 1 column 1
>> pwd
ans = C:\Users\Administrator
>> cd 'C:\Users\Desktop
parse error:
syntax error
>>> cd 'C:\Users\Desktop
^
>> who
Variables in the current scope:
A V ans sz
>> whos
Variables in the current scope:
Attr Name Size Bytes Class
==== ==== ==== ===== =====
A 3x2 48 double
V 1x4 32 double
ans 1x22 22 char
sz 1x2 16 double
Total is 34 elements using 118 bytes
>> V
V =
1 2 3 4
>> save hello.mat V
>> load heelo.mat
error: load: unable to find file heelo.mat
>> load hello.mat
>> who
Variables in the current scope:
A V ans sz
>> whos
Variables in the current scope:
Attr Name Size Bytes Class
==== ==== ==== ===== =====
A 3x2 48 double
V 1x4 32 double
ans 1x22 22 char
sz 1x2 16 double
Total is 34 elements using 118 bytes
>> clear V
>> whos
Variables in the current scope:
Attr Name Size Bytes Class
==== ==== ==== ===== =====
A 3x2 48 double
ans 1x22 22 char
sz 1x2 16 double
Total is 30 elements using 86 bytes
>> load hello.mat
>> whos
Variables in the current scope:
Attr Name Size Bytes Class
==== ==== ==== ===== =====
A 3x2 48 double
V 1x4 32 double
ans 1x22 22 char
sz 1x2 16 double
Total is 34 elements using 118 bytes
>> save hello.txt V -ascii %save as text (ASCII)
>> save hello1.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 =
3 4
>> A(2,:)% ":" means every element along the row/column
ans =
3 4
>> A(:,3)
error: A(_,3): but A has size 3x2
>> A(:,2)
ans =
2
4
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]]%Append another column vector to the right
A =
1 10 100
3 11 101
5 12 102
>> size(A)
ans =
3 3
>> A(:)
ans =
1
3
5
10
11
12
100
101
102
>> A(:)%put all the elements of A into a single vecotr
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 = [11 12; 13 14; 15 16]
B =
11 12
13 14
15 16
>> C = [A B]
C =
1 2 11 12
3 4 13 14
5 6 15 16
>> C = [A; B]
C =
1 2
3 4
5 6
11 12
13 14
15 16
>> size(C)
ans =
6 2
>> clear
>> clear all
>> A =[1 2; 3 4; 5 6]
A =
1 2
3 4
5 6
>> B = [11 12;13 14;15 16]
B =
11 12
13 14
15 16
>> C = [1 1;2 2]
C =
1 1
2 2
>> A * C
ans =
5 5
11 11
17 17
>> A .* B
ans =
11 24
39 56
75 96
>> 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
>> 1./A
ans =
1.00000 0.50000
0.33333 0.25000
0.20000 0.16667
>> log(v)
error: 'v' undefined near line 1 column 5
>> log(V)
ans =
0.00000
0.69315
1.09861
>> 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 % -1 * V
ans =
-1
-2
-3
>> V + ones(length(v),1)
error: 'v' undefined near line 1 column 17
>> V + ones(length(V),1)\
warning: using continuation marker \ outside of double quoted strings is deprecated and will be removed from a
future version of Octave, use ... instead
V + ones(length(V),1)
parse error:
syntax error
>>> V + ones(length(V),1)
^
>> V + ones(length(V),1)
ans =
2
3
4
>> V + ones(3,1)
ans =
2
3
4
>> V + 1
ans =
2
3
4
>> A
A =
1 2
3 4
5 6
>> A'
ans =
1 3 5
2 4 6
>> (A')'
ans =
1 2
3 4
5 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 < 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
>> A = magic(6)
A =
35 1 6 26 19 24
3 32 7 21 23 25
31 9 2 22 27 20
8 28 33 17 10 15
30 5 34 12 14 16
4 36 29 13 18 11
>> [r,c] = find(A>=13)
r =
1
3
5
2
4
6
4
5
6
1
2
3
4
6
1
2
3
5
6
1
2
3
4
5
c =
1
1
1
2
2
2
3
3
3
4
4
4
4
4
5
5
5
5
5
6
6
6
6
6
>> A = magic(3)
A =
8 1 6
3 5 7
4 9 2
>> [r,c] = find(A>=6)
r =
1
3
1
2
c =
1
2
3
3
>> A(2,3)
ans = 7
>>
>> a
a =
1.00000 15.00000 2.00000 0.50000
>> sum(a)
ans = 18.500
>> prod(a)
ans = 15
>> help prod
'prod' is a built-in function from the file libinterp/corefcn/data.cc
-- prod (X)
-- prod (X, DIM)
-- prod (..., "native")
-- prod (..., "double")
Product of elements along dimension DIM.
If DIM is omitted, it defaults to the first non-singleton
dimension.
The optional "type" input determines the class of the variable used
for calculations. If the argument "native" is given, then the
operation is performed in the same type as the original argument,
rather than the default double type.
For example:
prod ([true, true])
=> 1
prod ([true, true], "native")
=> true
On the contrary, if "double" is given, the operation is performed
in double precision even for single precision inputs.
See also: cumprod, sum.
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.
>> floor(a)
ans =
1 15 2 0
>> ceil(a)
ans =
1 15 2 1
>>
>> rand(3)
ans =
0.41404 0.48802 0.51386
0.82281 0.29080 0.18107
0.91562 0.97558 0.55899
>> max(rand(3),rand(3))
ans =
0.78646 0.51068 0.82123
0.57172 0.80178 0.73765
0.75045 0.65813 0.69859
>> A
A =
8 1 6
3 5 7
4 9 2
>> max(A,[],1)
ans =
8 9 7
>> MAX(A,[],2)%PER ROW
error: 'MAX' undefined near line 1 column 1
>> max(A,[],2) % per row
ans =
8
7
9
>> max(max(A))
ans = 9
>> A(:)
ans =
8
3
4
1
5
9
6
7
2
>> 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(8)
ans =
Diagonal Matrix
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
>> 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
>> help flipud
'flipud' is a function from the file E:\Octave\OCTAVE~1.0\share\octave\4.4.0\m\general\flipud.m
-- flipud (X)
Flip array upside down.
Return a copy of X with the order of the rows reversed. In other
words, X is flipped upside-down about a horizontal axis. For
example:
flipud ([1, 2; 3, 4])
=> 3 4
1 2
See also: fliplr, flip, rot90, rotdim.
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.
>> 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
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
>>
뽸