Notes from Octave

What does ~ do in Octave?

Depending on the syntax context it is

  • the logical not operator

  • a dummy placeholder in order to ignore arguments

https://octave.org/doc/v4.4.1/Ignoring-Arguments.html

https://octave.org/doc/v4.2.0/Element_002dby_002delement-Boolean-Operators.html

Octave Does not respect array index range

v = [1 2 3 4];
i = 1;
while i<=10,
    v(i) = 100;
    i++;
end;
display(v)
100   100   100   100   100   100   100   100   100   100   100   100   100   100   100   100   100   100   100   100

Function Handles: What is @(t) fname(t,x,y) ?

In octave, a function handle (i.e. pointer) is syntactically represented by the @ character. For example

function p = ff (n)
    p = 1;
    for i=1:n
        p = p*i;
    endfor
endfunction

my_factorial = @ff

my_factorial(10)

ans =  3628800

An anonymous function is definded by the syntax @(params) = ….

So it is a handle to a function definition without name. It is like python’s lambda.

So, in function call like the following:

X = [...]
y = [...]
A = fminunc( @(t)(constFunction(t, X, y)), ...)

the first parameter given to fminunc is an anonumous function with one parameter, t, which calls function constFunction with three parameters, t, X, y where X,y are like global variables.