Octave Cheatsheet

Octave

Commands:

Arithmetics: +, -, *, /, ^
Logical operations: ==, ~= (not equal), &&, ||, xor
Change prompt: PS1('>> ');
Semicolon: suppressing output
A = [1 2; 3 4; 5 6] - a 3x2 matrix
B = [1; 2; 3; 4; 5] - a 5x1 matrix
v = 1:0.1:2 - row matrix from 1 to 2 with 0.1 increment
ones(2, 3) - 2x3 matrix of all 1s
zeros(1,3) - 1x3 matrix of all 0s
w = rand(1,3) - 1x3 matrix of all random numbers
hist(w) - histogram
eye(4) - 4x4 identity matrix
help eye - how to use 'eye' function
size(A) = [2, 3] - 1x2 matrix for size (number of element) of row and size of column of A
size(A,1) = 2 - size of row of A
size(A,2) = 3 - size of column of A
length(A) = 3 - size of longest dimension of A
pwd - current direction/path currently in
load filename.dat - load data into a variable called 'filename'
who - list all variables in the current scope/workspace
whos - similar to who but with details such as Size, Bytes, Class
clear - delete all variables in current scope
clear variableName - remove variable named 'variableName' from current scope
B(1:2) = [1; 2] - return a column vector of first 2 elements of column vector B
A(1:2) = [1; 3] - return a column vector of first 2 elements of first column of A
save hello.txt A - save data of A into a file called 'hello.txt' in current directory in compressed format
save hello.txt A -ascii - save to file in human readable format
A(1,2) = 2 - element in 1st row and 2nd column
A(2,:) = [3 4] - return 2nd row of A
A(:, 2) = [2; 4; 6] - return 2nd column of A
"A(:, 2) = [20; 40; 60]" - replace 2nd column of A with [20; 40; 60] column vector
"A = [A, [200; 400; 600]" - add a 3rd column to A
A(:) - put all elements of A into a column vector
C = [A B] = [A, B] - concatenate 2 matrix into 1 column-wise
C = [A; B] = concatenate 2 matrix into 1 row-wise

Element-wise Operations:

Let A = [1 2; 3 4; 5 6], B = [11 12; 13 14; 15 16], C = [1 1; 2 2], v = [1; 2; 3]
A * B is invalid, but A .* B (element-wise multiplication) is valid and result in a 3x2 matrix
A .^ 2 - raise square (similarly for other arithmetic operator)
log(v) -  log base 10
exp(v) - base e power
abs(v) - absolute value
-v - element-wise invert sign
A' - A transpose, a 2x3 matrix. (A')' = A
max(v) - largest element of vector
max(A) = max(A,[],1) = [5, 6] - element-wise largest element by column
max(A,[],2) - element-wise largest element by row
[val, ind] = max(v) - value and index of largest element
v < 2 = [1; 0; 0] - compare each element, return 1 if true, 0 if false
A = magic(3) - each row, each column, each diagonal add up to the same sum
sum(A) - adds up all column elements of A, return a row vector
prod(A) - multiply all column elements of A, return a row vector
floor(A) - round down
ceil(A) round up



Comments

Post a Comment