51 lines
804 B
Matlab
51 lines
804 B
Matlab
% if statement
|
|
a = randi(100);
|
|
|
|
if a > 66
|
|
disp('a is greater than 66')
|
|
elseif (a > 33) && (a <= 66)
|
|
disp('a is between (33, 66]')
|
|
else
|
|
disp('a is between 1 and 33')
|
|
end
|
|
|
|
% switch/case
|
|
choices = ['A', 'B', 'C', 'D'];
|
|
idx = mod(a, numel(choices)) + 1;
|
|
|
|
switch choices(idx)
|
|
case 'A'
|
|
disp('A choosen')
|
|
case 'B'
|
|
disp('B choosen')
|
|
case 'C'
|
|
disp('C choosen')
|
|
otherwise
|
|
disp('Invalid choice')
|
|
end
|
|
|
|
% for statement
|
|
|
|
% this is intuitive
|
|
for i = 1:3:10
|
|
disp("i = " + string(i));
|
|
end
|
|
|
|
% this as well
|
|
v = [1 2 3];
|
|
for el = v
|
|
disp("el = " + string(el));
|
|
end
|
|
|
|
% when you iterate on a matrix, you iterate by columns
|
|
A = [1 2; 3 4; 5 6];
|
|
for column = A
|
|
disp(column);
|
|
end
|
|
|
|
% to iterate by rows
|
|
for row = A.'
|
|
disp(row);
|
|
end
|
|
|
|
% while loop: too easy |