Matrices

Nothing, but vectors in 2D.

In [1]:
A = [1, 2, 3; 2, 3, 5; 1, 1, 2] % commas separate horizontally, semicolons vertically
A =

   1   2   3
   2   3   5
   1   1   2

In [2]:
A = [1:5; 2:6; 3:7] % semicolons to separate rows
A = [(1:5)', (2:6)', (3:7)'] % commas to separate columns
A =

   1   2   3   4   5
   2   3   4   5   6
   3   4   5   6   7

A =

   1   2   3
   2   3   4
   3   4   5
   4   5   6
   5   6   7

In [3]:
v1 = [1, 2, 3];
v2 = 1:3;
v3 = linspace(1, 3, 3);
A = [v1; v2; v3]
A =

   1   2   3
   1   2   3
   1   2   3

Most people use in-built functions for $\texttt{zeros, ones, rand}$ creating matrices

In [4]:
A1 = zeros(4, 4)
A2 = ones(2, 3)
A3 = rand(5, 2)
A4 = ones(5) % be careful, this will create a matrix 5x5, not a vector
v = ones(1, 10) % set one dimension to 1 for a vector
A1 =

   0   0   0   0
   0   0   0   0
   0   0   0   0
   0   0   0   0

A2 =

   1   1   1
   1   1   1

A3 =

   0.135052   0.439202
   0.027999   0.977458
   0.630265   0.876276
   0.459083   0.889254
   0.365663   0.957965

A4 =

   1   1   1   1   1
   1   1   1   1   1
   1   1   1   1   1
   1   1   1   1   1
   1   1   1   1   1

v =

   1   1   1   1   1   1   1   1   1   1

Accessing elements of matrices

Just like vectors, but now provide two indices

In [5]:
A = [1, 2, 3; 4, 5, 6; 7, 8, 9]
A(2, 1) % second row, first column (rows always go first)
A =

   1   2   3
   4   5   6
   7   8   9

ans =  4
In [6]:
A(2, 1:3) % select an entire second row
A(2, 1:end) % select an entire second row
A(2, :) % select an entire second row
ans =

   4   5   6

ans =

   4   5   6

ans =

   4   5   6

You can use linear indices (providing only one index), it will select elements from a matrix one by one going columns frist, so

In [7]:
A(1)
A(2)
A(3)
A(4)
A(5)
A(6)
A(7)
A(8)
A(9) % normally don't write code like this, use a for loop (lecture 2)
ans =  1
ans =  4
ans =  7
ans =  2
ans =  5
ans =  8
ans =  3
ans =  6
ans =  9

Strings

Nothing, but vectors of characters

In [8]:
'a' % a character
ans = a
In [9]:
'abc'  % a vector of characters, always use single quotes ' '
ans = abc
In [10]:
greeting = 'Hello World!'
greeting = Hello World!

You can form matrices of strings (vectors of characters), but that's usually a bad idea, since the matrix dimensions have to match.

In [11]:
M = [greeting; greeting]
M =

Hello World!
Hello World!

In [12]:
new_greeting = 'Hi!';
M = [new_greeting; greeting]; % this gives an error in matlab, octave adds spaces after 'Hi!' to make dimensions work

Cell Arrays

Lists for everything whatsoever, a much better idea when storing multiple strings together (don't use matrices/vectors for strings).

Cell arrays are formed and accessed like normal vectors (or matrices), but all parenthesis become curly braces.

In [13]:
c = {'Hi', 2, 'you', [pi, 2; 4, 5]}
c = 
{
  [1,1] = Hi
  [1,2] =  2
  [1,3] = you
  [1,4] =

     3.1416   2.0000
     4.0000   5.0000

}
In [14]:
c = {2, 3; 4, 6} % two dimensional cell array (notice the semicolon)
c = 
{
  [1,1] =  2
  [2,1] =  4
  [1,2] =  3
  [2,2] =  6
}
In [15]:
c = cell(1, 5) % you can create empty cell arrays with cell(rows, columns)
c = 
{
  [1,1] = [](0x0)
  [1,2] = [](0x0)
  [1,3] = [](0x0)
  [1,4] = [](0x0)
  [1,5] = [](0x0)
}

Getting help

In [16]:
help abs % for quick reference
'abs' is a built-in function from the file libinterp/corefcn/mappers.cc

 -- abs (Z)
     Compute the magnitude of Z.

     The magnitude is defined as |Z| = 'sqrt (x^2 + y^2)'.

     For example:

          abs (3 + 4i)
               => 5

     See also: arg.

Additional help for built-in functions and operators is
available in the online version of the manual.  Use the command
'doc <topic>' to search the manual index.

Help and information about Octave is also available on the WWW
at http://www.octave.org and via the help@octave.org
mailing list.

Google 'matlab ' for a reference about a function.

Matlab has an amazing documentation online.

Alternatively, launch the docs from the program.

$\verb|doc <function>|$