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
$A=\begin{pmatrix}
1 & 2 \\
3 & 4 \\
5 & 6
\end{pmatrix}$
2. v = [1 2 3]
1 by 3 matrix (row vector)
$v=\begin{pmatrix}
1 & 2 & 3
\end{pmatrix}$
3. v = [1; 2; 3]
3 by 1 matrix (column vector)
$v=\begin{pmatrix}
1\\
2\\
3
\end{pmatrix}$
4. v=1:0.1:2
1 by 11 matrix (row vector)
$v=\begin{pmatrix}
1 & 1.1 & 1.2 & … & 1.9 & 2
\end{pmatrix}$
5. v=1:6
1 by 6 matrix (row vector)
$v=\begin{pmatrix}
1 & 2 & 3 & 4 & 5 & 6
\end{pmatrix}$
6. ones(2,3)
2 by 3 matrix of ones
$\begin{pmatrix}
1 & 1 & 1\\
1 & 1 & 1
\end{pmatrix}$
7. 2*ones(2,3)
$\begin{pmatrix}
2 & 2 & 2\\
2 & 2 & 2
\end{pmatrix}$
8. zeros(2,3)
2 by 3 matrix of zeroes
$\begin{pmatrix}
0 & 0 & 0\\
0 & 0 & 0
\end{pmatrix}$
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
$\begin{pmatrix}
1 & 0 & 0 & 0\\
0 & 1 & 0 & 0\\
0 & 0 & 1 & 0\\
0 & 0 & 0 & 1
\end{pmatrix}$
Plot histogram: w = -6 + sqrt(10)*(randn(1,10000))
hist(w)
histogram
hist(w,50)
histogram with 50 bins
Help: help rand
help function