Reading — step 1 of 5
Learn
~1 min readLinear Algebra and I/O
Octave was made for linear algebra. The basics:
Matrix construction:
A = [1 2; 3 4] % 2x2
B = zeros(3) % 3x3 zeros
C = eye(4) % 4x4 identity
D = ones(2, 3) % 2x3 of ones
E = rand(3, 3) % 3x3 random in [0,1)
Element-wise vs matrix multiply:
A * B % matrix multiplication
A .* B % element-wise
A / B % matrix divide (right): solves x*B = A
A \ B % matrix divide (left): solves A*x = B -- common for linear systems
A ./ B % element-wise
A ^ 2 % matrix power
A .^ 2 % element-wise square
Transpose — A' (or A.' for non-conjugate). Important for complex.
Common operations:
det(A) % determinant
inv(A) % inverse
A\b % solve A*x = b (better than inv(A)*b)
eig(A) % eigenvalues
[V, D] = eig(A) % eigenvectors V, eigenvalues D
rank(A) % rank
norm(A) % matrix norm
trace(A) % sum of diagonal
Slicing:
A(1, :) % first row
A(:, 2) % second column
A(1:2, 2:3) % submatrix
A(end, end) % last element
A(end-1, :) % second-to-last row
Reshape without copying data:
v = 1:12;
M = reshape(v, 3, 4) % 3x4
Solving Ax = b is the killer feature — A\b uses LU decomposition under the hood, no manual algorithm needed.
Discussion
Ask a question, share an insight, or help someone who’s stuck.
Sign in to post a comment or reply.
Loading…