MATLAB

Appeared in:
late 1970s
Influenced by:
Paradigm:
Typing discipline:
File extensions:
.m
Versions and implementations (Collapse all | Expand all):
Programming language

MATLAB (from matrix laboratory) is an interpreted programming language for solving computational problems. The language is matrix-oriented (hence the unofficial language motto “Think Vectorized”). It provides built-in implementations of lots of useful algorithms, rich plotting and visualization capabilities and methods of interfacing with other languages.

The language was created in late 1970s. Initially it was used to enable working with FORTRAN libraries without knowledge of FORTRAN itself. MATLAB quickly gained recognition and popularity among people who were into applied mathematics.

Elements of syntax:

Inline comments %
Nestable comments %( ... %)
Case-sensitivity yes
Variable identifier regexp [a-zA-Z][_a-zA-Z0-9]*
Variable assignment varname = value
Variable declaration global varname
Grouping expressions ( ... )
Block foo ... end, where foo is one of { if, for, while, ... }
Physical (shallow) equality == eq isequal
Physical (shallow) inequality ~= ne
Comparison < > <= >=
Function definition function retval = f(para1, para2) retval = ...
Function call f(a, b, ...)
Function call with no parameters f
Sequence , ;
If - then if condition, ..., end
If - then - else if c1, b1, elseif c2, b2, else, b3, end
While condition do while c, ..., end
For each value in a numeric range, 1 increment for i = 1:10, ..., end
For each value in a numeric range, 1 decrement for i = 10, 1, -1 do ... end

Examples:

Hello, World!:

Example for versions GNU Octave 3.2.3

The first function is identical to the C one. The second one is Octave-specific.

printf("Hello, World!\n");

disp("Hello, World!");

Factorial:

Example for versions GNU Octave 3.2.3

This example uses built-in function factorial. Note that at this scale of magnitude the results are exact, but in general case Octave is not meant for arbitrary-precision computations, so huge values of factorial will be calculated approximately.

for i = 0 : 16
  printf("%d! = %d\n", i, factorial(i));
endfor

Factorial:

Example for versions GNU Octave 3.2.3

This example uses iterative factorial definition. Semicolons at the end of lines suppress the automated output of the calculated values (in this case of fact) in interactive mode, so that the formatted output doesn’t get littered.

fact = 1;
for i = 0 : 16
  printf("%d! = %d\n", i, fact);
  fact *= i+1;
endfor

Factorial:

Example for versions GNU Octave 3.2.3

This example uses recursive factorial definition.

function f = fact(n)
  if (n <= 1)
    f = 1;
  else
    f = n * fact(n - 1);
  endif
endfunction

for i = 0 : 16
  printf("%d! = %d\n", i, fact(i));
endfor

Fibonacci numbers:

Example for versions GNU Octave 3.2.3

This example uses recursive definition of Fibonacci numbers.

function f = fib(n)
  if (n <= 1)
    f = n;
  else
    f = fib(n - 1) + fib(n - 2);
  endif
endfunction

for i = 1 : 16
  printf("%d, ", fib(i));
endfor
disp("...");

Quadratic equation:

Example for versions GNU Octave 3.2.3

Octave is suited for numeric computations, so it has built-in methods of solving typical problems, including finding the roots of a polynomial. To do this one can call roots function for a vector of polynomial coefficients listed in order of descending powers (so the coefficient at the highest power is listed first).

roots([2 -3 1])