Introduction

Environment

This snippet's purpose is to get familiarized with basic MATLAB's interface and tools, specially working with vectors and matrices.

The command window is where you type your commands.

Daily commands

If you ever used a Linux terminal, many commands are basically the same, for example:

• ls or dir list files under this directory.
• cd change directory.
• pwd return working directory (where you are at the moment).
• rmdir delete empty directory. Use s to delete non-empty directories. For example: rmdir mydir s.
• delete delete file.
• help + command obtain help about that command.

Vectors and matrices

The name of MATLAB comes from "Matrix Laboratory". That's because everything is a matrix in MATLAB, even numbers (1x1 matrix) and vectors (1xN matrix for row vectors and Nx1 for column vectors):

>> v = [1 2 3 4]

To convert it to a column vector:

>> w = v.'
w =
1
2
3
4

Accesing an element:

>> v(2)
ans =
2

The variable ans is an auxiliary used by MATLAB when you don't make any assignement to your own variables. To return a range:

>> v(2:3)
ans =
2	3

>> v(2:end)
ans =
2	3	4

You can make jumps:

v =
1	2	3	4	5	7	8	9	10

>> v(1:2:end)
ans =
1	3	5	7	9

You can use another vector as index of a vector

>> v( [1 4 7] )
ans =
1	4	7

To define a 3x3 matrix:

>> A = [1 2 3; 4 5 6; 7 8 9]
A =
1	2	3
4	5	6
7	8	9

To select an element:

>> A(2,3)
ans =
6

To select a column vector:

>> A(1:2,3)
ans =
3
6

To select a row vector:

>> A(2,:)
ans =
4	5	6

To select a submatrix:

>> A(1:2,:)
ans =
1	2	3
4	5	6

To make all elements in the third column equal to zero:

>> B = A;
B(:,3) = 0
B =
1	2	0
4	5	0
7	8	0

To jump columns:

>> A(1:end,1:2:end) = 0
B =
1	2	0
4	5	0
7	8	0

A logical matrix is made of 1s and 0s, where 1 represents "true" and 0 represents "false":

>> D = logical([1 0 0; 0 0 1; 0 0 0])
D =
1	0	0
0	0	1
0	0	0

>> A(D)
ans =
1
6

To have all matrix elements in a column vector:

>> A(:)
ans =
1
4
7
2
5
8
3
6
9

To know the dimensions of a matrix:

>> A = [1 1 1; 1 2 3]
A =
1	1	1
1	2	3

>> [M, N] = size(A)
M =
2
N =
3

Standard arrays

• zeros(M,N) MxN matrix of zeros of double type.
• ones(M,N) MxN matrix of ones of type double.
• true(M,N) MxN logical matrix of all ones.
• false(M,N) MxN logical matrix of all zeros.
• magic(M) MxM magic-square matrix.
• rand(M,N) MxN of random numbers in the interval [0,1].
• randn(M,N) same as previous but normally distributed (gaussian, for example).

Writing .m files

An .m file is MATLAB's batch file, i.e. a text file where you can write your commands and then load it in your workspace and run it to execute those commands. To write those files you can use MATLAB's editor. To call the editor, type editor in the command window. To run an .m file, go to the directory where the file is and type its name.

Example of .m file:

% Read an image in this directory
f = imread('audi.jpg');

% Save the image's size
% M is the number of rows
% N is the number of columns
% c is the number of colors
[M, N, c] = size(f);

% Detailed info (don't use ; or it won't print anything)
whos f

Any line that starts with a % is a comment and is ignored. If you end a line with ;, it will be executed but nothing will be printed to the console.

Control structures

The for loop

The syntax of a for loop in MATLAB is:

for index = start : increment: finish
<what you want to execute n times>
end

For example:

% Example of for loop
A = [1 1 1; 1 1 1; 1 1 1]
for q = 0:1:3
A = A*A
end

The if - elseif - else statement

The syntax of conditional statements in MATLAB is:

if expression 1
statement 1
elseif expression 2
statement 2
else
statement 3
end

An example using the function disp() to write things to the console:

% Example of conditional sentences
for q = 0:1:4
if q == 0
disp('q = 0')
elseif q == 1
disp('q = 1')
elseif q == 2
disp('q = 2')
else
disp('q is not 0, 1 or 2')
end

The while loop

The syntax of a while loop in MATLAB is:

while expression
sentences
end

For example:

% Example of while loop
a = 10;
b = 5;

while a
a = a - 1;
disp('loop a')
while b
b = b - 1;
disp('loop b')
end
end

The switch conditional

The syntax of the switch conditional in MATLAB is:

switch expression
case case1
sentences
case case2
sentences
...
otherwise
sentences
end

You can place as many cases as you need. The moment a case matches the expression, MATLAB will leave the switch. For example:

% Example of the switch conditional
for q = 0:1:4
switch q
case 0
disp('q = 0')
case 1
disp('q = 1')
case 2
disp('q = 2')
case 3
disp('q = 3')
otherwise
disp('q is not 0, 1, 2 or 3')
end
end