Loops and Conditionals

  • $\textbf{randi}$ returns an ndarray of random intergers between the given range
In [1]:
a = randi([0 10],1,8)
a =

    9   10   10    1    7    7    7    3

  • A way of printing out each element in $\textrm{a}$ by iterating over the length using $\textbf{for}$ loop
In [2]:
for i=a
fprintf("%f \t", i);
end
9.000000 	10.000000 	10.000000 	1.000000 	7.000000 	7.000000 	7.000000 	3.000000 	
In [3]:
S=size(a); 
fprintf("%d \t", S);
1 	8 	
  • The size of the array is obtained, where the first element shows the number of rows $N_r$ and the second element shows the number of columns $N_c$.
  • for this particular example $N_r=1$ and $N_c=8$.
  • This shows the maximum length of the array
In [4]:
L=length(a);
fprintf("L= %d \t\n", L);
L= 8 	
  • This example shows the generation of random array and printing each element of the array using the $\textbf{for}$ loop in a systematic manner
In [5]:
b = randi([0 10],1,8);
for i=1:length(b) # length(a) = 8
    fprintf("b[%d", i); 
    fprintf("]= \t"); 
    fprintf("%d\n", b(i)); 
end
b[1]= 	2
b[2]= 	1
b[3]= 	0
b[4]= 	7
b[5]= 	0
b[6]= 	2
b[7]= 	8
b[8]= 	4
  • This shows the elements inside the array $\textrm{k}$ which varies from 1 to maximum length of array $\textrm{b}$.
In [6]:
k=1:length(b)
k =

   1   2   3   4   5   6   7   8

Creation of a 2D array and printing the elements through a loop

  • A $ 4\times 5$ matrix $\textrm{c}$ is created whose elements can vary form $0$ to $10$
In [7]:
c = randi([0 10],4,5); disp(c);
   1   0   9   8   5
   9   3   6   1   3
   3   6   6   5   1
   1   2   2   2   5
  • In this example a $\textbf{nested}$ $\textbf{for}$ loop is used to print all the elements of the 2D matrix.
  • In the outer loop the code loops over all the columns and in the inner loop it prints all the elemets of a column.
In [8]:
# This is a nested loop
for i=c # Loops over all the columns
    for j=i # Loops over all the elements of a column
        fprintf("%d\t",j);
    end
end
1	9	3	1	0	3	6	2	9	6	6	2	8	1	5	2	5	3	1	5	
  • The $\textbf{length(c)}$ command gives the maximum value of row or column number for a 2D matrix.
In [9]:
length(c)
ans =  5
  • The $\textbf{size(c)}$ command gives the number of rows and columns of the 2D matrix.
In [10]:
size(c)
ans =

   4   5

  • The $\textbf{numel(c)}$ command gives the number elements of the 2D matrix.
In [11]:
numel(c)
ans =  20

Some conditionals on arrays

In [12]:
d = [0   6    4   3    1   0    1    7];
disp(d);
disp(1:length(d));
   0   6   4   3   1   0   1   7
   1   2   3   4   5   6   7   8
  • The array $\textrm{d}$ contains $8$ elements which include $2$ zeros and $2$ ones as shown in the above example.
  • We want to print values of the array $\textrm{d}$ until we reach the first one.
In [13]:
for i=1:length(d)
    if (d(i) == 1)
        break;
    endif
        fprintf("%d \n", d(i))
endfor
0 
6 
4 
3 
  • In the above example, $\textbf{for}$ loop with $\textbf{if}$ and $\textbf{break}$ conditional statements are used to print the values of the array $\textrm{d}$ until we reach the first one.
  • Then we proceed to count the number of zeros in the array $\textrm{d}$
In [14]:
fprintf ("%d \t\n", d);
counter= 0;
for i =1:length(d);
    if (d(i) == 0)
        counter = counter + 1;
        fprintf("zero encountered at location: %d \t\n", i);
      endif
      endfor
fprintf("counter = %d \t", counter);
0 	
6 	
4 	
3 	
1 	
0 	
1 	
7 	
zero encountered at location: 1 	
zero encountered at location: 6 	
counter = 2 	
  • In the above example $\textbf{for}$ loop with $\textbf{if}$ conditionals are used to calculate the number of zeros in the array
  • This example also highlights the location of zero encounterd and the number of zeros is given by the variable $\textrm{counter}$.

Sorting an array using bubble sort

In [15]:
e= randi([0 20],1,7); 
fprintf("%d\n", e);
L=length(e);
fprintf("Length of the array is = %d\n", L);
disp(1:length(a)) 
i = 1;
disp(0:length(a)-1)
5
20
19
20
19
3
19
Length of the array is = 7
   1   2   3   4   5   6   7   8
   0   1   2   3   4   5   6   7
  • A random array $\textbf{e}$ is created whose length is 7.
  • The array $\textbf{e}$ needs to be sorted using $\textit{bubble-sort}$.
  • In the following code, $\textit{bubble-sort}$ algorithm is employed which is: Compare the first and the second element of the array and swap them if they are in wrong order. Compare the second and the third element of the array and swap them if they are in wrong order. Proceed till the last element of the array in a similar fashion. Repeat all of the above steps until the array is sorted.
In [16]:
n=length(e);
for i=1:1:n-1
    for j=1:1:n-1    
      if e(j)>e(j+1);
        temp=e(j);
        e(j)=e(j+1);
        e(j+1)=temp;
      else
        fprintf("No swap required \n")
    endif
    disp(e)
    endfor
endfor
No swap required 
    5   20   19   20   19    3   19
    5   19   20   20   19    3   19
No swap required 
    5   19   20   20   19    3   19
    5   19   20   19   20    3   19
    5   19   20   19    3   20   19
    5   19   20   19    3   19   20
No swap required 
    5   19   20   19    3   19   20
No swap required 
    5   19   20   19    3   19   20
    5   19   19   20    3   19   20
    5   19   19    3   20   19   20
    5   19   19    3   19   20   20
No swap required 
    5   19   19    3   19   20   20
No swap required 
    5   19   19    3   19   20   20
No swap required 
    5   19   19    3   19   20   20
    5   19    3   19   19   20   20
No swap required 
    5   19    3   19   19   20   20
No swap required 
    5   19    3   19   19   20   20
No swap required 
    5   19    3   19   19   20   20
No swap required 
    5   19    3   19   19   20   20
    5    3   19   19   19   20   20
No swap required 
    5    3   19   19   19   20   20
No swap required 
    5    3   19   19   19   20   20
No swap required 
    5    3   19   19   19   20   20
No swap required 
    5    3   19   19   19   20   20
    3    5   19   19   19   20   20
No swap required 
    3    5   19   19   19   20   20
No swap required 
    3    5   19   19   19   20   20
No swap required 
    3    5   19   19   19   20   20
No swap required 
    3    5   19   19   19   20   20
No swap required 
    3    5   19   19   19   20   20
No swap required 
    3    5   19   19   19   20   20
No swap required 
    3    5   19   19   19   20   20
No swap required 
    3    5   19   19   19   20   20
No swap required 
    3    5   19   19   19   20   20
No swap required 
    3    5   19   19   19   20   20
No swap required 
    3    5   19   19   19   20   20
  • In the above example $\textbf{for}$ loop with $\textbf{if}$ conditionals are used for swapping the elements and $\textit{temp}$ variable is used to temporarily save the elements being compared.
  • In $\textit{Octave}$ the array can be directly sorted using the funtion $\textit{sort}$ as shown in the above example.
In [17]:
f= randi([0 20],1,7); 
disp(f);
disp(sort(f));
   19   13   18    2    3    0    3
    0    2    3    3   13   18   19
In [ ]: