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

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...

Add/Embed SVG to Blogger website

In this post I will tell you my method(trick) of adding SVG images in a blogger website or blog. Before starting , the first thin g I am assu m ing is that you are aware of SVG if you are here. If not please see S calable V ec tor G raphics Recently when I tried to embed a SVG image for a post on pygal, I tried uploading the SVG file and blogger Image uploader came up with an error, because of which I had to find some other way.  SVG File upload Error in Blogger  I started sea rc hing Google " Embed SVG in Blogger " . I found blogorrhea , w h ich gave some i nformatio n on add ing SVG directly as a markup , which worked , but I faced another problem using this . Also th is guy has used lot of Javascript which was confusin g for me, being new to using SVG.   So I first t houg ht of learning on h ow to embed SVG in HTML and t his on e worked out. Actually we can embed SVG in HTML i n following ways: Using Object tag Using Iframe tag Using embed...

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 documenta...