GNU Octave Part 2 - Basic

SSMD & maths

Aung Baw
2 min readMar 22, 2021
  1. REPL & self help, if you want to change primary prompt string just type, put anything you want to be as a cursor.
PS1('$ ')  % changing primary prompt string to $% this is just a comment
# this one too
help command_name;
% place any command to get help doc offline
clear
% command clears out all declared variables

2. SSMD, You knew it if you’d programmed in any programming language at all, but will repeat it here anyway.

1 + 1  % sum
1 - 2 % subtraction
3 * 4 % multiplication
9 / 3 % division
3 \ 9 % left division (just flip-flop)
n5 ^ 5 % power

We need to extra careful about . [dot] in octave, we could use like following —

m = [1 2; 3 4]; % m is 2x2 matrix 
m * m; % will get matrix multiplication while
m .* m; % will get each individual cell multiplied

we will see more example in next article when we deep dive into matrices.

3. Constants, that might be useful to you.

# Math constants likepi      % 3.1416...
e % 2.7183
realmax % largest floating point number
realmin % smallest floating point number
eps % system-dependent, machine precision
# Others
I, i, J, j % a pure imaginary number
Inf, inf % Infinity
NaN, nan % Not a number

4. Variable and data types

Please refer to this doc look for the version you just installed on your system. For starter build in data types are numeric (like decimal, floating point & range of integer), text (string), logical (), structure (work like object in other programming language, esp C), others like (NA not available & NaN not a number)

5. Vectors & Matrices, we are here for some math and visual plotting right

  • Row or column oriented vector
x = [1 2 3 4 5]     % note that unlike array from others language there is no comma , between each roomy = [1; 2; 3; 4; 5] % ; separate each row so it's single column
  • Matrices

Let’s recall basic from math class, in 2D algebra it’s x then y, (x,y) right? consider x as a row and y as a column, its gonna make your vector more readable and make sense at glance.

a = [1 2; 3 4];      % two rows(x) and two columns(y)
a = [1 2 3; 4 5 6]; % two rows(x) and three columns(y)
# NOTE that ; inside the square bracket indicate end of one row

6. Comparison & Boolean operators

Usual operators like greater than, less than, equal, not equal, bitwise & | ! ~. Only thing to note in this session is ‘not equal’ operator.

b != a   % other programming language
b ~= a % octave

--

--

Aung Baw

Focusing on security, cloud, and DevOps, I am a lifelong learner and lazy 徒弟.