Contents

Quick primer on some insightful characteristics of matlab for the course

MATlab : Matrix Laboratory can be used as a scripting language any variable is a matrix (a scalar is a special case of a multidimensional array)

credits Alessandro Giusti alessandrog@idsia.ch

Giacomo Boracchi September 23rd, 2019

close all
clear
clc

disp('For enquiry, please send an email to giacomo.boracchi@polimi.it');
fprintf('there is also a C-like printing function\n%s %dst, %d', 'October', 1, 2018);
For enquiry, please send an email to giacomo.boracchi@polimi.it
there is also a C-like printing function
October 1st, 2018

variables

% variables are created by assignements
v = 3
c = 'k'
size(v)

% use ; at the end of instruction not to display results
% in command line
v = 7;

% data types are automatically defined depending on the
% assigned value
whos v

% variables have datatypes (usually you can forget,
% but not when dealing with images).  Most common types we
% will consider are:
% . double,(i.e. double-precision floating point),
% . uint8 (i.e. unsigned integers with 8 bits, [0 255] range),
% . logical (i.e. boolean)

%
% casting to 8-bit integers
v = uint8(v)
whos v
v =

     3


c =

    'k'


ans =

     1     1

  Name      Size            Bytes  Class     Attributes

  v         1x1                 8  double              


v =

  uint8

   7

  Name      Size            Bytes  Class    Attributes

  v         1x1                 1  uint8              

Arrays

a row vector (commas can be omitted)

r=[1, 2, 3, 4]
size(r)

% a column vector
c=[1; 2; 3; 4]
size(c)

% you can define vectors by regular increment operator
% [start : step : end]
a = [1 : 2 : 10];

% when omitted, step is equal to 1
a = [1 : 10];

% a matrix
v=[1 2; 3 4]
size(v)
r =

     1     2     3     4


ans =

     1     4


c =

     1
     2
     3
     4


ans =

     4     1


v =

     1     2
     3     4


ans =

     2     2

Array concatenation

you can concatenate matrices or vectors as far as their sizes are consistent

B=[v', v']

C = [v ; v]

% this is not allowed
disp('K = [r,c]: this is not allowed')
%K = [r,c]
B =

     1     3     1     3
     2     4     2     4


C =

     1     2
     3     4
     1     2
     3     4

K = [r,c]: this is not allowed

other examples of array concatenation

dim returns the dimension of the data, visible also typing whos

dim = size(C)

% other examples of array concatenation
my_vec1 = [1 2 3];
my_vec2 = 4:6;

my_matrix = [my_vec1; my_vec2]
size(my_matrix)

my_long_vector = [my_vec1 my_vec2]
size(my_long_vector)

my_matrix = cat(1,my_vec1,my_vec2)
my_long_vector = cat(2,my_vec1,my_vec2)

my_3d_matrix = cat(3,my_vec1,my_vec2)
size(my_3d_matrix)
dim =

     4     2


my_matrix =

     1     2     3
     4     5     6


ans =

     2     3


my_long_vector =

     1     2     3     4     5     6


ans =

     1     6


my_matrix =

     1     2     3
     4     5     6


my_long_vector =

     1     2     3     4     5     6


my_3d_matrix(:,:,1) =

     1     2     3


my_3d_matrix(:,:,2) =

     4     5     6


ans =

     1     3     2

indexing (starts from 1)

my_vec = (1:10)';
my_vec(1)

my_matrix = [1:4;5:8;9:12];

my_matrix(3,2)

% coumn-wise linear indexing for matrices
my_matrix(6)
ans =

     1


ans =

    10


ans =

    10

subarray

v(indexes) returns a vector of all the elements of v at locations in array indexes

% you can reference single or multiple values in an array:
v(1,2) % first row and second column (row and column indices are 1-based)
v(:,2) % the second column of v
v(1,:) % the first row
B(:,2:4) % some of the columns
v(5,5) = 10 % you can extend vectors / matrices by assigning some element our of the range

my_matrix(1:3,3:4)

my_matrix(1:3,[2 4])

my_matrix(1:end,[2 4])

my_matrix(:,[2 4])

my_matrix(1:2,[2 4])

my_matrix(1:end-1,[2 4])

my_vector = my_matrix(:)
ans =

     2


ans =

     2
     4


ans =

     1     2


ans =

     3     1     3
     4     2     4


v =

     1     2     0     0     0
     3     4     0     0     0
     0     0     0     0     0
     0     0     0     0     0
     0     0     0     0    10


ans =

     3     4
     7     8
    11    12


ans =

     2     4
     6     8
    10    12


ans =

     2     4
     6     8
    10    12


ans =

     2     4
     6     8
    10    12


ans =

     2     4
     6     8


ans =

     2     4
     6     8


my_vector =

     1
     5
     9
     2
     6
    10
     3
     7
    11
     4
     8
    12

operation on vectors are from linear algebra

common operations work on matrices (careful about multiplication and division)

v*v'
ans =

     5    11     0     0     0
    11    25     0     0     0
     0     0     0     0     0
     0     0     0     0     0
     0     0     0     0   100

there are also element-wise operators

[1 2 3].*[4 5 6]

[1 2 3] + 5
[1 2 3] * 2 % no need to use element-wise operator with scalars
[1 2 3] .* 2 %explicit element-wise multiplication
[1 2 3] / 2
[1 2 3] ./ 2 %explicit element-wise division
[1 2 3] .^ 2 %explicit element-wise power

[1 2 3] * [4 5 6]' % this matrix product, returns a scalar (it is the inner product)

[1 2 3]' * [4 5 6] % this is the matrix product, returns a 3 x 3 matrix

% rounding functions
ceil(10.56)
floor(10.56)
round(10.56)

ceil(0:0.1:1)

% arithmetic functions
sum([1 2 3 4])
sum([1:4;5:8])
sum([1:4;5:8],2)

sum(sum([1:4;5:8]))

my_matrix = [1:4;5:8];
sum(my_matrix(:))
ans =

     4    10    18


ans =

     6     7     8


ans =

     2     4     6


ans =

     2     4     6


ans =

    0.5000    1.0000    1.5000


ans =

    0.5000    1.0000    1.5000


ans =

     1     4     9


ans =

    32


ans =

     4     5     6
     8    10    12
    12    15    18


ans =

    11


ans =

    10


ans =

    11


ans =

     0     1     1     1     1     1     1     1     1     1     1


ans =

    10


ans =

     6     8    10    12


ans =

    10
    26


ans =

    36


ans =

    36

Printing

disp('Hello World!');
disp(['Hello ', 'World!']);

% string concatenation in printf.
disp(['number of columns in the matrix: ', num2str(size(my_matrix))]);

fprintf('number of columns in the matrix: %f\n',size(my_matrix,2));
Hello World!
Hello World!
number of columns in the matrix: 2  4
number of columns in the matrix: 4.000000

logicals

you can compare a vector via the customary relational operators and obtain a vector of logicals (i.e. b)

b=v>2
whos b
b =

  5×5 logical array

   0   0   0   0   0
   1   1   0   0   0
   0   0   0   0   0
   0   0   0   0   0
   0   0   0   0   1

  Name      Size            Bytes  Class      Attributes

  b         5x5                25  logical