Most optimization functions in Octave such as fminunc expect vectors the parameter to be optimized. However, sometimes (e.g. in the field of neural networks) the vector representation is somewhat unnatural and clumsy to work with. Converting between vector and matrix representation is referred to as unrolling (matrix -> vector) and reshaping (vector -> matrix).

Unrolling

Unrolling is quite simple. Given a matrix A, you have two different possibilities of addressing the elements of A. Either you address rows and colums separately, i.e., A(i,j) or you use one single index or index range A(k) which indexes A column by column (Remember that the colon stands for the range from 1 to end):

>> A = [1 2 3; 4 5 6]
A =
   1   2   3
   4   5   6
>> v = A(:)
v =
   1
   4
   2
   5
   3
   6

As you can see, A(:) is exactly what we want: A vector representation of the matrix.

Reshaping

The reverse way, reshaping, is performed by a command called reshape. It takes as arguments the vector to be reshaped and the row and column count of the target matrix. Continuing the example from above, we get the following:

>> Ares = reshape(v, 2, 3)
Ares =

1   2   3
4   5   6

Unrolling and reshaping with multiple matrices

Say you have multiple matrices,  A,B, and C. Then the described procedure works analogously:

>> A = [1 1; 1 1]; B = [2 2]; C = [3;3];
>> v = [A(:); B(:); C(:)]
v =
   1
   1
   1
   1
   2
   2
   3
   3
>> Ares = reshape(v(1:4), 2, 2)
Ares =
   1   1
   1   1
>> Bres = reshape(v(5:6), 1, 2)
Bres =
   2   2
>> Cres = reshape(v(7:8), 2, 1)
Cres =
   3
   3

Working with files

Load CSV file into a matrix. This command loads file ex2data.txt and stores its contents in a variable named ex2data:

load ex2data1.txt

Matrices and Vectors

Appending to a vector (does not work on matrices as this would break the rectangular shape):

a = [1; 2; 3];
a(end + 1) = 4;

Plotting

Create empty figure:

figure;

Plot XY diagram using “+” markers in black:

plot(X, Y, "k+");

Plot multiple datasets in one XY diagram:

figure
hold on
plot(X,Y,"k+") % draw with '+' markers
plot(A,B,"ko") % draw with 'o' markers
hold off % release figure

Random

Create a random permutation of the integers 1 to 10:

v = randperm(10); % produces a row vector of size 1x10

The same function can be used for drawing without replacement, e.g., out of a number of items 1,2,…,10 take five distinct elements:

v = randperm(10, 5); % produces a row vector of size 5

Utility functions

Change default prompt (PS1):

PS1('>> ')

Change/check working directory:

pwd; %print working directory
cd '/tmp'; % change working directory to /tmp

What variables are currently defined/visible:

who; % short
whos; %long

Continue command in next line by typing three dots

cd(...'/tmp');

Reading user input from the shell:

x = input(“Please enter the value of x: “);

Miscellaneous:

  • Commands can be concatenated with a comma (produce output) or semicolon (hide output).
  • Reverse i search works just like for the Bash

Links

  • [1] Octave documentation