Short syntax overview
if, elseif, else
Execute statements if condition is true.
Documentation
if condition
statements
elseif another_condition
statements
else
statements
end
for loop
Repeat statement for each element in a vector. At each iteration it puts next element from the vector
values
to the variable
value
.
Documentation
for value = values
statements
end
while loop
Repeat statement when condition is true.
Documentation
while condition
statements
end
function
Declare a function with name
name
, where
y1,...,yN
are output variables (put your variables instead of
...
) and
x1,...,xM
are input variables.
Documentation
function [y1,...,yN] = name(x1,...,xM)
Code examples
Finding the maximum element in a vector
clear;
v = [1, 4, 7, 2, 19];
maximum = v(1);
for i = 1:length(v)
element = v(i);
if element > maximum
maximum = element;
end
end
disp('The maximum is: ');
disp(maximum);
Print "Hello world!" 5 times
clear;
v = 1:1:5;
for element=v
disp('Hello world!');
end
Sum of elements in a vector
clear;
v = [1, 4, 61, 4];
sum = 0;
for element=v
sum = sum + element;
end
disp(sum);
Sum of elements in a vector using indices (the same as previous)
clear;
v = [1, 4, 61, 4];
sum = 0;
for index=1:1:length(v)
element = v(index);
sum = sum + element;
end
disp(sum);
Sum of elements in each column of a matrix
clear;
M = [1, 3;
2, 2;
4, 7;
1, 1];
number_of_rows_in_M = size(M, 1);
number_of_cols_in_M = size(M, 2);
sum = zeros(1, number_of_cols_in_M);
indices_rows = 1:1:number_of_rows_in_M;
indices_cols = 1:1:number_of_cols_in_M;
for row_index=indices_rows
row = M(row_index, :);
for col_index=indices_cols
element = row(col_index);
% element = M(row_index, col_index); % alternative approach
sum(col_index) = sum(col_index) + element;
end
end
disp(sum);
Or alternative code:
clear;
M = [1, 3;
2, 2;
4, 7;
1, 1];
number_of_rows_in_M = size(M, 1);
number_of_cols_in_M = size(M, 2);
sum = zeros(1, number_of_cols_in_M);
for row_index=1:1:number_of_rows_in_M
row = M(row_index, :);
for col_index=1:1:number_of_cols_in_M
element = row(col_index);
% element = M(row_index, col_index); % alternative approach
sum(col_index) = sum(col_index) + element;
end
end
disp(sum);
Simple if elseif else example
clear;
a = 1; % try to change
b = 1; % try to change
if a == 0 && b == 0
disp('if');
elseif b > 0 || a < 0
disp('elseif');
else
disp('else');
end
All pairs of vector elements
clear;
numbers = [1, 4, 6, 7, 10];
for v_index = 1:length(numbers)-1
v = numbers(v_index);
for w_index = v_index+1:length(numbers)
w = numbers(w_index);
pair = [v, w];
disp(pair);
end
end
Product of two numbers using function
The function has two input variables:
a
and
b
. The result of these two numbers product is stored in the variable called
prod
, hence it is output of the function. Please note that you cannot run the script with function, since a function needs concrete input to work. You should have only one function in a file.
File
product.m
:
function prod=product(a, b)
prod = a*b;
end
After you created a function, you can execute it from any other place by passing input values.
File
calculate_products.m
:
p1 = product(1,2);
disp(p1);
p2 = product(3,5);
disp(p2);
a = 12;
b = 23;
p3 = product(a, b);
disp(p3);
p4 = product(2, b);
disp(p4);