Computing Data (Octave)
Matrices
1. A = [1 2; 3 4; 5 6]
$A=\begin{pmatrix}
1 & 2 \\
3 & 4 \\
5 & 6
\end{pmatrix}$
B = [11 12; 13 14; 15 16]
$B=\begin{pmatrix}
11 & 12 \\
13 & 14 \\
15 & 16
\end{pmatrix}$
C = [1 1; 2 2]
$C=\begin{pmatrix}
1 & 1 \\
1 & 2
\end{pmatrix}$
2. A*C
$\begin{pmatrix}
5 & 5\\
11 & 11 \\
17 & 17
\end{pmatrix}$
3. A .* B
take each element of A to multiply by each element of B
$\begin{pmatrix}
11 & 24\\
39 & 56 \\
75 & 96
\end{pmatrix}$
4. A .^ 2
square each element of A
$\begin{pmatrix}
1 & 4\\
9 & 16 \\
25 & 36
\end{pmatrix}$
5. v = [1; 2; 3]
$v=\begin{pmatrix}
1\\
2\\
3
\end{pmatrix}$
6. 1 ./ v
element-wise reciprocal of v
$v=\begin{pmatrix}
1.00000\\
0.50000\\
0.33333
\end{pmatrix}$
7. log(v)
element-wise logarithm of v
exp(v)
element-wise exponential of v
abs(v)
element-wise absolute value of v
-v
element-wise negative value of v
v+1
element-wise addition of 1 to v
8. A = [1 2; 3 4; 5 6]
$A=\begin{pmatrix}
1 & 2 \\
3 & 4 \\
5 & 6
\end{pmatrix}$
A'
transpose of A
$\begin{pmatrix}
1 & 2 & 3 \\
4 & 5 & 6
\end{pmatrix}$
9. w = [1 15 2 0.5]
$w=\begin{pmatrix}
1 & 15 & 2 & 0.5
\end{pmatrix}$
10. max (w)
maximum value of w
val = 15
11. [val, ind] = max(w)
maximum value of w and index where it is located
val = 15
ind = 2
12. w < 3
element-wise comparison of whether w is less than 3
$\begin{pmatrix}
1 & 0 & 1 & 1
\end{pmatrix}$
13. find(w < 3)
find which elements that variable w is less than 3
$\begin{pmatrix}
1 & 3 & 4
\end{pmatrix}$
14. sum(w)
sum of w
ans = 18.5
15. prod(w)
product of w
ans = 15
16. floor(w)
rounds down elements of w
$\begin{pmatrix}
1 & 15 & 2 & 0
\end{pmatrix}$
17. ceil(w)
rounds down elements of w
$\begin{pmatrix}
1 & 15 & 2 & 1
\end{pmatrix}$
18. A = magic(3)
magic square of 3 by 3
$\begin{pmatrix}
8 & 1 & 6\\
3 & 5 & 7\\
4 & 9 & 2
\end{pmatrix}$
19. [r,c] = find(A >= 7) find rows and columns of A greater than or equal to 7
$r=\begin{pmatrix}
1\\
3\\
2
\end{pmatrix}$
$c=\begin{pmatrix}
1\\
2\\
3
\end{pmatrix}$
20. A(2,3)
$\begin{pmatrix}
7
\end{pmatrix}$
21. max(A,[],1)
column-wise maximum of A
$\begin{pmatrix}
8 & 9 & 7
\end{pmatrix}$
22. max(A,[],2)
row-wise maximum of A
$\begin{pmatrix}
8 \\
7 \\
9
\end{pmatrix}$
23. max(max(A))
$\begin{pmatrix}
9
\end{pmatrix}$
24. pinv(A)
inverse of A
$\begin{pmatrix}
0.147 & -0.144 & 0.064 \\
-0.061 & 0.022 & 0.106 \\
-0.019 & 0.189 & -0.103
\end{pmatrix}$