This article records my Octave learning notes, keeps developing…
Chinese Version: Octave是Matlab开源版本,这篇文章记录我对Octave学习心得和基本使用方法,以便以后手生的时候快速上手。
Basic Operations
-
tilde equal ~=
is in octave, Octave usesbang equal !=
-
Octave uses
single quote
to present string assignmentb = ‘hi’
-
Like Matlab, semicolon suppressing(抑制) output.
disp(str)
for printing string.disp(sprintf(‘2 decimals: %0.2f’, a))
for printing format string.- format long/short
- row vector [1 2 3]
- column vector [1; 2; 3]
ones(2,3)
=> initialise matrixrand(1,3)
=> uniform distribution from 0 to 1: gaussian distribution with mean zero and variance/standard deviation equal to one; gaussian random number/ normal random number.I = eye(5)
=> Identity Matrix- help eye
- example
1
w = -6 + sqrt(10)*(randn(1,10000000)); hist(w) hist(w,50) => print 50bucket
Moving Data Around
-
size, length
1
size(A) = rows columns size(A,1) = rows size(A,2) = columns length(A) = longest dimension
-
load data, find data
- pwd {show current path}/ cd ‘path’ / ls
- difference between mat and dat
- load file.data = load(‘file.dat’)
- who/ whos{describe the details}
- clear / clear variable
- save hello.mat {dat?}
- v = priceY(1:10) => the first 10 elements, column order
-
I(1:5) / I(1,3)
octave:47> B = I(1:5)
B =
1 0 0 0 1
octave:48> I
I =
Diagonal Matrix1 0 0
0 1 0
0 0 1 -
save file.mat v; {binary format, compress?}
-
save file.txt v -ascii; % save as text (ASCII)
-
manipulation
- A(3,2) A(2,:) % “:” means every element along that row/column
- A([1 3; :]) % all first and third columns
- Append
A is a 3 by 2 matrix
A = [A, [100; 200; 300] ]
Now, A is 3 by 3
A = [A; [100, 200, 300]]
Now, A is 4 by 3 - A(:) % put all elements of A into a single column vector
Now, A is a 12 by 1 column vector - concatenation
C = [A B] / [A, B]
C = [A; B] % ; means go to the next line
Computing on data
-
element-wise operation
. .^ ./
log(v) exp(v) abs(v) -v %equals -1v
v + 1 % v + ones(length(v),1) -
Matrix transpose,
A = (A’)’
-
Function
max
1
a is a row vector: max(a) [value, index] = max(a) A is a matrix: max(A) % column wise max
-
logic
1
a = [1 15 2 0.5] a < 3 = [1 0 1 1] find(a < 3) % list the index of element satisfy the given condition ans = [1 3 4]
-
magic(n)
=> create an N by N magic square, magic(2) is not defined.magic square
=> each row, each column, and diaglog sum up the same value1
octave:64> A A = 8 1 6 3 5 7 4 9 2 octave:65> find(A>=5) {return the index checking by column} ans = 1 5 6 7 8 octave:66> [r c]=find(A>=5) r = 1 2 3 1 2 c = 1 2 2 3 3
-
column-wise & element-wise
1
2sum(A) prod(A) {A is matrix, column wise}
floor(A) round up {element-wise} -
rand(1,3)% create 1 by 3; rand(3)% create 3 by 3
-
max(A,B) % A and B has same size, maximum on element wise
1
octave:84> A = magic(3) A = 8 1 6 3 5 7 4 9 2 octave:85> max(A) ans = 8 9 7 octave:86> max(A,[],1) % = max(A), 1 means the first dimension, column ans = 8 9 7 octave:87> max(A,[],2) % 2 means the second dimension, row ans = 8 7 9 octave:89> max(A,5) ans = 8 5 6 5 5 7 5 9 5 octave:90> min(A,5) ans = 5 1 5 3 5 5 4 5 2 find the entire maximum is A: max(max(A)) or max(A(:))
-
Function
Sum
1
A = magic(9) sum(A,1) % sum column sum(A,2) % sum row sum the main diagonal sum(sum(A .* eye(9))) sum the other diagonal: using function flipud, flip up an down sum(sum(A .* flipud(eye(9))))
-
sudep inverse
pinv(A)1
octave:93> A A = 8 1 6 3 5 7 4 9 2 octave:94> inv(A) ans = 0.147222 -0.144444 0.063889 -0.061111 0.022222 0.105556 -0.019444 0.188889 -0.102778 octave:95> pinv(A) ans = 0.147222 -0.144444 0.063889 -0.061111 0.022222 0.105556 -0.019444 0.188889 -0.102778 octave:96> A*ans ans = 1.0000e+00 -1.2212e-14 6.5503e-15 5.5511e-17 1.0000e+00 -1.1102e-16 -5.8842e-15 1.2434e-14 1.0000e+00 octave:97> inv(A) ans = 0.147222 -0.144444 0.063889 -0.061111 0.022222 0.105556 -0.019444 0.188889 -0.102778 octave:98> A*ans ans = 1.00000 0.00000 -0.00000 -0.00000 1.00000 0.00000 0.00000 0.00000 1.00000
Plotting Data
- Plot 1
1
octave:131> x = [0:0.01:1]; octave:132> plot(x) octave:133> xlabel('x number times') octave:134> ylabel('value of x') octave:135> legend('x') octave:136> title('plot1') octave:137> print -dpng 'plot1.png’
- Plot 2
1
close figure(1); plot… figure(2); plot…. subplot(1,2,1) %Divides plot a 1x2 grid, access the first element axis([ 0.5 1 - 1 1]); clf; % clear figure
- Draw Matrix
1
2
3
4
5
6octave:173> magic(3)
ans =
8 1 6
3 5 7
4 9 2
octave:174> plot(magic(3))
1 |
imagesc(magic(5)), colorer, colormap gray; {commands/comma chaining of function } |
Control Statement, Functions
- while true,
break; - Octave search path
addpath(path); - function [y1,y2] = …. % return multiple values
Vectorization
numerical and linear algebra library in octave, C++, java
Cost Function Example
Vectorisation Octave Code
Vectorisation C++ Code
Linear Regression Example
近期评论