1. basics
octave:1> 5+6
ans = 11
octave:2> 3-2
ans = 1
octave:3> 1/2
ans = 0.50000
octave:4> 2^6
ans = 64
octave:5> 1 == 2 % false
ans = 0
octave:6> 1 ~= 2
ans = 1
octave:7> 1 && 0 % AND
ans = 0
octave:8> 1 || 0 % OR
ans = 1
octave:9> xor(1,0)
ans = 1
octave:10> PS1('>> ')
>> a = 3
a = 3
>> a = 3; % semicolon supressing output
>> 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))
2 decimals: 3.14
>> disp(sprintf('6 decimals: %0.6f', a))
6 decimals: 3.141593
>> format long
>> a
a = 3.141592653589793
>> format short
>> a
a = 3.1416
>>
>>
>> % vector & matrix
>> 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
>> 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 7:
1.0000 1.1000 1.2000 1.3000 1.4000 1.5000 1.6000
Columns 8 through 11:
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
>> 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.032946 0.091541 0.915109
>> rand(3,3)
ans =
0.935449 0.616362 0.234641
0.291993 0.030991 0.439985
0.562570 0.420936 0.756866
>> rand(3,3)
ans =
0.40589 0.77029 0.28754
0.69338 0.55964 0.73797
0.82374 0.62061 0.58714
>> rand(3,3)
ans =
0.47307 0.86448 0.65171
0.35197 0.23997 0.63824
0.28821 0.35546 0.80000
>> w = randn(1,3)
w =
-1.49816 0.34803 0.35071
>> w = -6 + sqrt(10)*(randn(1,10000));
>> 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(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 eye
2. moving data
>> A = [1 2; 3 4; 5 6]
A =
1 2
3 4
5 6
>> size(A)
ans =
3 2
>> sz = size(A);
>> size(sz)
ans =
1 2
>> size(A,1)
ans = 3
>> size(A,2)
ans = 2
>> v = [1 2 3 4]
v =
1 2 3 4
>> length(v)
ans = 4
>> length(A)
ans = 3
>> length([1;2;3;4;5])
ans = 5
>> who
Variables in the current scope:
A I ans sz v w
>> load test.txt
>> who
Variables in the current scope:
A I ans test sz v w
>> size(test)
ans =
97 2
>> whos
Variables in the current scope:
Attr Name Size Bytes Class
==== ==== ==== ===== =====
A 3x2 48 double
I 6x6 48 double
ans 1x2 16 double
test 97x2 1552 double
sz 1x2 16 double
v 1x4 32 double
w 1x10000 80000 double
Total is 10244 elements using 81712 bytes
>> clear test
>> whos
Variables in the current scope:
Attr Name Size Bytes Class
==== ==== ==== ===== =====
A 3x2 48 double
I 6x6 48 double
ans 1x2 16 double
sz 1x2 16 double
v 1x4 32 double
w 1x10000 80000 double
Total is 10050 elements using 80160 bytes
>> v = w(1:10)
v =
-4.4952 -3.5211 -8.1121 -10.4763 -6.0211 -11.4582 -5.0729 -7.0230 -10.7224 -4.4823
>> whos
Variables in the current scope:
Attr Name Size Bytes Class
==== ==== ==== ===== =====
A 3x2 48 double
I 6x6 48 double
ans 1x2 16 double
sz 1x2 16 double
v 1x10 80 double
w 1x10000 80000 double
Total is 10056 elements using 80208 bytes
>> save hello.mat v;
>> clear v
>> whos
Variables in the current scope:
Attr Name Size Bytes Class
==== ==== ==== ===== =====
A 3x2 48 double
I 6x6 48 double
ans 1x2 16 double
sz 1x2 16 double
w 1x10000 80000 double
Total is 10046 elements using 80128 bytes
>> load hello.mat
>> v
v =
-4.4952 -3.5211 -8.1121 -10.4763 -6.0211 -11.4582 -5.0729 -7.0230 -10.7224 -4.4823
>> save hello.txt v -ascii
>> A = [1 2; 3 4; 5 6]
A =
1 2
3 4
5 6
>> A(3, 2)
ans = 6
>> A(2,:) % colon means every element along that row/column
ans =
3 4
>> 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 column
error: horizontal dimensions mismatch (3x2 vs 1x3)
>> A = [A, [100; 101; 102]] % append column
A =
1 10 100
3 11 101
5 12 102
>> size(A)
ans =
3 3
>> A(:) % put all into a single column vector
ans =
1
3
5
10
11
12
100
101
102
>> 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
>> [A, B]
ans =
1 2 11 12
3 4 13 14
5 6 15 16
3. computing
>> 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)
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
v =
1
2
3
>> v + ones(length(v),1)
ans =
2
3
4
>> length(v)
ans = 3
>> ones(3,1)
ans =
1
1
1
>> 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(2)
val = 2
ind = 1
>> max(A)
ans =
5 6
>> a
a =
1.00000 15.00000 2.00000 0.50000
>> 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
>> [r, c] = find(A >= 7)
r =
1
3
2
c =
1
2
3
>> A(2,3)
ans = 7
>> help find
>> 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
>> rand(3)
ans =
0.944515 0.429266 0.060716
0.539757 0.112316 0.097833
0.557747 0.329321 0.016222
>> max(rand(3), rand(3))
ans =
0.91996 0.56297 0.65726
0.48438 0.90429 0.73187
0.33574 0.87792 0.63489
>> 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 .* 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 .* flipud(eye(9))))
ans = 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
>> 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+00 8.3267e-17 -2.9421e-15
-5.9952e-15 1.0000e+00 6.3838e-15
2.9976e-15 4.4409e-16 1.0000e+00
4. plotting
>> t=[0:0.01:0.98];
>> y1=sin(2*pi*4*t);
>> plot(t,y1)
>> 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'
>> subplot(1,2,1); % divides plot a 1x2 grid, access first element
>> plot(t,y1)
>> subplot(1,2,2);
>> plot(t,y2)
>> axis([0.5 1 -1 1])
>> 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)
>> clf;
>> imagesc(A), colorbar, colormap gray;
>> a=1, b=2, c=3
a = 1
b = 2
c = 3
>> a=1; b=2; c=3;
5. control
>> 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
>> 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
>> while i <= 5,
> v(i) = 100;
> i = i + 1;
> end;
>> v
v =
100
100
100
100
100
64
128
256
512
1024
>> v(1)
ans = 999
>> v(1) = 2;
>> if v(1) == 1,
> disp('value one');
> elseif v(1) == 2,
> disp('value two');
> else
> disp('not one or two');
> end;
value two
6. function
single return value
In current directory, create the following .m file
// squareThisNumber.m
function y = squareThisNumber(x)
y = x^2;
back in octave
>> squareThisNumber(5)
ans = 25
multiple return values
In current directory, create the following .m file
// squareAndCubeThisNumber.m
function [y1,y2] = squareAndCubeThisNumber(x)
y1 = x^2;
y2 = x^3;
back in octave
>> [a,b] = squareAndCubeThisNumber(5);
>> a
a = 25
>> b
b = 125
reference
https://www.coursera.org/learn/machine-learning/home/week/2