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:
such that it has 10x2 as its matrix dimensions. The file is available for download from here: data.txt .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 |
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
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
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