%% Homework #1
%Blocks can be made in MATLAB using %%
%% Problem #1
%Printing my name
fprintf('Mohamadreza Fazel\n')
%% Problem #2
%Creating an array of zeros and assigning numbers to it using a for loop
A = zeros(10,1)
for ii = 1:10 %loop from 1 to 10. 'ii' increments each time through the loop
A(ii) = ii; %Use ';' to supress A during the for loop
end
A %Show 'A' in the command window
%% Problem #3
%Counting even numbers
Even = 0; %number of the even numbers
for ii = 1:10
%There are many ways to do this logic. Here is one way
B = floor(A(ii)/2); %the output is A(ii)/2 rounded down to the nearest integer
if (B - (A(ii)/2)) == 0
Even = Even + 1; % when the if statement is true this line will be executed
end
end
Even
%% Problem #4
%Finding the first ten odd integers
NtoFind = 10; % Its good practice to set as a variable so if you want to change it later, you change it once.
Odd = zeros(NtoFind,1);
ii = 0; %this is the input number and will be raised by one in each iteration
NFound = 0; %this index counts the odd numbers
while NFound < NtoFind
ii = ii + 1; %This is the number to be tested for 'oddness'
C = floor(ii/2); %this gives the integer part of (ii/2)
if (C - (ii/2)) ~= 0
NFound = NFound+1;
Odd(NFound) = ii; %Odd is a vector and we will add a component to it when if-statement is true
end
end
Odd