Basic Operations (Octave)
Logical operations
1. 1 == 2
1 equals to 2
ans = 0
false
2. 1 ~= 2
1 is not equal to 2
ans = 0
true
3. 1 && 2
AND
ans = 0
4. 1 || 2
OR
ans = 1
5. xor(1,0)
ans = 1
Change default octave prompt: PS1('>> ');
Assign variables
1. a = 3
printing out a = 3
2. a = 3;
suppress print out
3. b = 'hi'
for strings
4. c = (3>=1)
true
Printing
1. disp(a)
to show a
2. a = pi
disp(sprintf('2 decimals: %0.2f', a))
2 decimal places
2 decimals: 3.14
2. disp(sprintf('2 decimals: %0.6f', a))
6 decimal places
2 decimals: 3.141593
3. format long
a
a = 3.14159265358979
4. format short
a
a = 3.1416
Matrices
1. A = [1 2; 3 4; 5 6]
3 by 2 matrix
2. v = [1 2 3]
1 by 3 matrix (row vector)
3. v = [1; 2; 3]
3 by 1 matrix (column vector)
4. v=1:0.1:2
1 by 11 matrix (row vector)
5. v=1:6
1 by 6 matrix (row vector)
6. ones(2,3)
2 by 3 matrix of ones
7. 2*ones(2,3)
8. zeros(2,3)
2 by 3 matrix of zeroes
9. rand(2,3)
2 by 3 matrix of random numbers between 0 and 1
10. randn(2,3)
2 by 3 matrix of random numbers drawn from a Gaussian distribution with mean 0 and variance
11. eye(4)
4 by 4 identity matrix
Plot histogram: w = -6 + sqrt(10)*(randn(1,10000))
hist(w)
histogram
hist(w,50)
histogram with 50 bins
Help: help rand
help function