Skip to main content

Rejecting the applications that are not fit using Matlab

Program

Let us say that you have got a client who is a HR Manager of a company. Understand the situation below.
The company has given a notification in a newspaper for an opening and around 10 people has turned up. The selection process was three stages out of which for two stages your client want you to sort out the applicants. In the first stage a question paper for 5 marks is given and in the second stage a question paper for 5 marks is given while third stage is interview. They store the marks of the candidates in a file which has the data which looks as follows:
Id Number Paper 1 Paper 2
1 1.5317 4.6950
2 2.5425 4.3797
3 2.5539 2.7508
4 4.0881 3.1124
5 3.9742 2.9352
6 3.2216 1.0387
7 1.8930 1.5062
8 4.0579 2.3546
9 2.6641 1.1524
10 1.7536 4.2215
such that it has 10x2 as its matrix dimensions. The file is available for download from here: data.txt .
Now what your client want is to sort out applications for the following conditions:
The applicant must have secured greater than or equal to 3 in Paper 1 and also greater than or equal to 2 marks in paper 2. Write a program in Matlab to sort out the people returning a matrix with the selected applicants marks and also neatly display how many of them were rejected while sorting.

Solution

To be frank this question is not very difficult to solve but it is some what lengthy for programmers like me. Also this can be solved in many ways. But we will stick to the for loop as we know the number of applicants. But remember that you will have to use while loop if the number of applicants is unknown or very large. You can also use while loop for this problem by starting a counter and checking if it is less than the number of applicants or any other way. But this will become a lengthy process when compared to for loop. So we will solve this problem using for loop.
I will explain how we are going to solve this problem. As the given data is in matrix form we will create a for loop and index the  first and second value of each row and use the and logic to check both the conditions. This should give us a good solution. We should also note that we will be using a counter to count the number of people eliminated.
Some of the concepts you need to know to solve this problem are as follows:
1) Delete row from Matrix - Matlab
2) Matlab Indexing
3) Loading Data into Matlab - Matlab load command
4) Matlab For loop
5) Execute statements specific number of times - for loop
6) Size (Matlab Functions)
7) Logical Or - Matlab
8) To create your own matrix of random numbers: Generate random matrix Matlab
9) To save the file of the matrix you have created: save(Matlab Functions).
10) While loop in matlab
11) if else conditionals in Matlab

Program



 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
load data.txt
 
deleted_rows = 0;

[rows, columns] = size(data); 
i = 1;
counter = 1;
while counter <= rows
    a = data(i,:);
    id_number = a(1);
    paper_one = a(2);
    paper_two = a(3);
    if paper_one <3 || paper_two <2
        fprintf('Eliminated person: %d\n',id_number);
        deleted_rows = deleted_rows+1;
        data(i,:) = [];
        i = i;
        counter = counter+1;
    else
        fprintf('Person Selected: %d\n',id_number);
        i = i+1;
        counter = counter+1;
    end
end

fprintf('\nNumber of people eliminated: %d\n',deleted_rows);
fprintf('People who have been selected and their marks is as follows: \n')
data

I have saved this program as selection.m on my local computer

Explanation

You may think on why we have to write a program for this data instead we can see the data and eliminate the person. There may be cases where there is no need for writing a program but I will tell you a few instances where there is a real need for these kind of programs. Consider a very big company like Microsoft. They will have to scrutinize 1000's of applications and I don't think men will be free to do such a simple task. Might be their program is an enhanced version of the above program but the basics are same. Also lets say you have conducted an online exam and you will have to announce the results, looking at the data and deleting each and every row will be a hectic work and also a waste of time so that time you will use this program. 
Line 1: We are loading the data using load command in matlab.
Line 3: We are creating a variable to store the number of rows that will be deleted.
Line 5: We are using the size function in Matlab to store the size of the Matrix. We are equating the size to its rows and columns. If you want to know more about the size of matrix then refer to : Matrix Size.
Line 6, 7: We are again creating two variables i and counter that we will use in the further part of program.
Line 8: We are initiating a while loop and the condition for the while loop is the value of counter should be less than the number of rows. So if we will increase the value of counter by 1 for every iteration then this loop will iterate for 10 times(size of row).
Line 9: We are indexing the data. Remember that after loading the file data.txt the matrix inside the data.txt is stored in data variable. This is the reason why we are using the variable data to index the first row. 
Line 10-12: We are splitting the id number and the corresponding marks.
Line 13-18: We are checking the marks and if the person doesn't qualify the requirements then we are deleting the row and increasing the counter variable so that the loop will run for the next iteration. We are not increasing the value of i because after we have deleted the row the rows below are shifted up and so virtually the next row number will be the same number of the present i. We are also increasing the value of deleted_rows so that the we can finally tell the management that these many people have been restricted.
Line 19-24: If the person is qualified then, these statements will be executed.
Line 26-28: We are displaying the management the data in neat and clean format.

Try it yourself

1) Create a file with 20 rows and four columns, the first columns being the id number, second columns being the paper 1, third columns being paper 2 and the fourth columns being paper 3. The lowest marks in all the papers should be 0 and the highest will be 10. Now write a program to eliminate the person for the following conditions:
paper 1 > 5
paper 2 > 3
paper 3 >1. Comment your program so that it will be useful for everyone.
2) Improve the above program so that the user will be displayed the paper in which he god failed. So that in future he/she can improve his performance. Comment your program in the comment box below so that it will be useful for everyone.
3) Try to write the above program using for loop. Comment in the comment box below.

Input/Output

selecting or disqualifying a person based on marks using matlab
Selection in a company using matlab

Final Note

I have tried to explain everything in this post in such a way that it is easy to understand for everyone. If you have not understood anything or have any doubt then please do contact me or comment in the comment box below. You can contact me from here: Contact me

Keywords: selection

Popular posts from this blog

Making a quiz web app with python and flask

Edit : When you are creating a web app with h tml templates, then y ou will have to sa ve the html file in templates folder in the Current Wor ki ng Directory( CWD). If you save the file in the C W D directl y you will get a TemplateNotFound error. Thank you Udhay for pointing it out.   In this post we will create a quiz website using python . I will be using the flask framework . After reading this tutorial you will learn form submission , flask templates , python code in flask templates , shuffling the questions and options with the random module and few others.  Please note that this tutorial is not big as it seems to be. Some of the code has been rewritten to maintain consistency and also font size is somewhat big so that your eyes won't get stressed reading this tutorial. Also the content has not occupied the full width of the page. In this tutorial I am assuming that you are having a very basic understanding of the flask framework . Please refer the documentation

Problem 11 Project Euler Solution with python

Largest product in a grid In the 20×20 grid below, four numbers along a diagonal line have been marked in red. 08 02 22 97 38 15 00 40 00 75 04 05 07 78 52 12 50 77 91 08 49 49 99 40 17 81 18 57 60 87 17 40 98 43 69 48 04 56 62 00 81 49 31 73 55 79 14 29 93 71 40 67 53 88 30 03 49 13 36 65 52 70 95 23 04 60 11 42 69 24 68 56 01 32 56 71 37 02 36 91 22 31 16 71 51 67 63 89 41 92 36 54 22 40 40 28 66 33 13 80 24 47 32 60 99 03 45 02 44 75 33 53 78 36 84 20 35 17 12 50 32 98 81 28 64 23 67 10 26 38 40 67 59 54 70 66 18 38 64 70 67 26 20 68 02 62 12 20 95 63 94 39 63 08 40 91 66 49 94 21 24 55 58 05 66 73 99 26 97 17 78 78 96 83 14 88 34 89 63 72 21 36 23 09 75 00 76 44 20 45 35 14 00 61 33 97 34 31 33 95 78 17 53 28 22 75 31 67 15 94 03 80 04 62 16 14 09 53 56 92 16 39 05 42 96 35 31 47 55 58 88 24 00 17 54 24 36 29 85 57 86 56 00 48 35 71 89 07 05 44 44 37 44 60 21 58 51 54 17 58 19 80 81 68 05 94 47 69 28 73 92 13 86 52 17 77 04 89 55 40 04 52 08 83 97 35 99 16 07

Problem 60 Project Euler Solution with python

Prime pair sets The primes 3, 7, 109, and 673, are quite remarkable. By taking any two primes and concatenating them in any order the result will always be prime. For example, taking 7 and 109, both 7109 and 1097 are prime. The sum of these four primes, 792, represents the lowest sum for a set of four primes with this property. Find the lowest sum for a set of five primes for which any two primes concatenate to produce another prime. This problem is j u st a brute force problem. If you have come here because you don't know the limit upto which you will h ave to gener ate the prime numbers t hen go ahe ad and t r y with 10,000 . When I first start ed solving the problem I chose 1 million(beca use most of the problem s on project E uler have this limit ), but it took very long for the computer to fin d the solution. After searching on the internet then I found many people choosing 10, 000 so I have changed my in put f rom 1 million to 10000 and the output was f ast. He