শ্রেণীঅনুসারে আর্কাইভ: ম্যাটল্যাব

MATLAB code for Major Minor grouping of students of Dept of EEE, BUET

It’s been some time since I joined BUET as a lecturer. Today, we were assigning major and minor topics to students. The assigning process was quite cumbersome and had to be done by hand. In order to automate the process, I wrote a small MATLAB program that can be used to automate the process.

The major minor system of the Dept of EEE is quite simple. Currently, there are 3 major groups and 4 minor groups of the department. Based on decision of BUET Undergraduta Studies (BUGS) there is a maximum number of student per major or minor group. The groups are allocated according to CGPA of the students, and if more students want a particular major group than allowed limit, then students with better CGPA will get more preference.

Any how, I am pasting my program here:

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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
% This program automates creation of major minor list of the students.
% Program was written by Sajid Muhaimin Choudhury, Lecturer, Department of
% EEE, BUET on 19/5/2010
 
clc
 
%constants:
N_maj_max = 60;
N_min_max = 60;
N_std = 180;
 
%Execute the program once, then open variable editor to edit rawdata and
% paste values from excel, columns are, roll, cgpa, maj_choice 1-4, minor
% choice 1-4. 
 
%rawdata = [1,2,3,4,5,6,7,8,9,10;4,5,6,7,8,9,10,11,12,5];
rawdata = sortrows(rawdata, -2); %sort according to CGPA
std_roll = rawdata(:,1)
std_cgpa = rawdata(:,2)
std_maj_choice = rawdata(:,3:6)
std_min_choice = rawdata(:,7:10)
 
std_maj = zeros(N_std,1);
std_min = zeros(N_std,1);
count_maj = zeros(1,4);
count_min = zeros(1,4);
%assign major group according to CGPA. 
for i = 1:N_std
    for j = 1:4
        if count_maj(std_maj_choice(i,j)) < N_maj_max
            count_maj(std_maj_choice(i,j)) = count_maj(std_maj_choice(i,j)) +1;
            std_maj(i) = std_maj_choice(i,j);
            break;
        end
   end
end
 
 
%assign minor group according to CGPA
for i = 1:N_std
    for j = 1:4
        if (count_min(std_min_choice(i,j)) < N_min_max) &&  (std_min_choice(i,j) ~= std_maj(i))
            count_min(std_min_choice(i,j)) = count_min(std_min_choice(i,j)) +1;
            std_min(i) = std_min_choice(i,j);
            break;
        end
   end
end
 
final_result = [std_roll'; std_cgpa' std_maj'; std_min']';
 
final_result = sortrows(final_result, 1);

মন্তব্য নেই

দক্ষ ম্যাটল্যাব কোডিং

(This article is inspired by Omar’s Article)

Most people programming at Matlab first get accustomed to some other programming languages like C, C++, Java or even Visual Basic. They learn tactics of manipulating arrays with loops. But as more and more loops are added to a program, the clumsier it gets. In Matlab also, the techniques learned at the previous mentioned languages can apply. But Matlab has some other advantages. In Matlab, every variable is a matrix. This gives some inherent advantages of matrix manipulation. In C, (and in C++ without having a custom class) you will not be able to do a matrix multiplication by simple A*B, in Matlab, you have a provision of doing so.

Ok let’s start with some basic array operations. These may not be arcane secrets, still it’s always good to know.

Identity Matrix, Zero Matrix and Unity Matrix

n = 3;
A = eye (n); %creates n x n identity matrix
A = eye (n, 1); %creates n x 1 identity matrix
A = zeros(n); % creates n x n zero matrix (all elements zero)
A = ones(n); %  creates n x n  matrix (all elements one)

A set of linearly increasing values

This one is useful if you are making a sine wave or other functions.  linspace (initialvalue, finalvalue, number of samples)

t = linspace (0, 2*pi, 1000); %creates linearly increasing array with values 0 to 2*pi
S = sin (t);  % creates the sine wave

Accessing a particular row or column

A(i,:) = 1 % makes all the elements of row i of matrix/vector A equal to 1.
A(:,i) = 1 % does the same thing with column 1.
B = A(i,:) % stores row i in a row vector B

Accessing more than one rows/columns simultaneously

A = ([1,
A([i,j],:) = 1 % makes all elements of rows i and j equal to 1
B = A(:,[i,j])% B is a matrix with columns i and j of A as its two columns

Swapping rows / columns

A([i,j],:) = A([j,i],:) % swaps the elements of rows i and j
A(:,[i,j]) = A(:,[j,i]) % swaps the column elements of col i and col j

Replacing Values of A

To make a unipolar signal bipolar:

A = double(A); % required if the signal is of boolean type
A (A==0) = -1;

The find( ) function

Used to find index of elements satisfying some condition.

find(A) % returns indices of all non-zero elements
find(A &gt; 5) % finds indices of all elements greater than 5.
length(find(A==1)) %number of ones in A

Inserting blank Rows / Columns:

A = [zeros(1,n-1); A]; % insert column
A = [A zeros(n,1)]; %insert Row

Repeating or tiling a Matrix

Use repmat() to tile matrix to form a larger version.

A = [1 2 3; 4 5 6]

A =

1 2 3
4 5 6

>> repmat(A, 3, 3)

ans =

1 2 3 1 2 3 1 2 3

4 5 6 4 5 6 4 5 6
1 2 3 1 2 3 1 2 3
4 5 6 4 5 6 4 5 6
1 2 3 1 2 3 1 2 3
4 5 6 4 5 6 4 5 6

Deleting a row/column of a matrix

A(i) = [] % Deletes the i-th element of a row/column vector A

A(i,:) = [] % Deletes an entire row
A(:,i) = [] % Deletes an entire row

Chaning Size of Matrix

Wanna make a 3×4 matrix a 2×6 Matrix?
use the reshape command

A = [2 3 4 5; 6 7 8 9]

A =

2 3 4 5
6 7 8 9

>> reshape (A, 4, 2)

ans =

2 4

6 8
3 5
7 9

If you want to make only a vector,
B = A(:);

Warning: reshape, *ALWAYS* takes data from columns, and put them in column serially. You may get unexpected result, if, you want the data to be taken from rows instead. Try to transpose the matrix (A') instead.

Ctrl+C. Break the running loop of Matlab.

You may need to select the command window first to activate this.

Making Plots pretty

http://blogs.mathworks.com/loren/2007/12/11/making-pretty-graphs/
http://blinkdagger.com/matlab/matlab-tips-and-tricks-on-creating-better-figures-and-plots
http://www.nada.kth.se/~hjorth/matlab/

Repeating Values of a Matrix

Kronecker Tensor Product

Use the Kronecker Tensor Product function (kron),

A = [1 2 3]

A =

1 2 3

>> kron (A, ones(1, 3))

ans =

1 1 1 2 2 2 3 3 3

Tony’s Trick

This is a popular (and allegedly faster) trick used to form a matrix by repeating a row/column vector. (Courtesy of omar)

B = A(ones(3,1),:)
*Example:
>> A =[1 2 3] % A is a row vector

>> B = A(ones(3,1),:)
>> B =

1 2 3
1 2 3
1 2 3

>> A = [1 ; 2 ; 3] % A as a column vector

>> B = A(:,ones(3,1))

>> B =

1 1 1
2 2 2
3 3 3

And WOW! it’s really faster for replication in one dimension! For matrix A given above:
Elapsed time is 0.014354 seconds. %Using repmat()function
Elapsed time is 0.000063 seconds. %Using Tony’s trick

Repeating Matlab row elements:

Based on the Tony’s trick mentioned above, this is a program I’ve written for the ‘time scaling’ a matrix by repeating elements.

 %time scaling
function y = timescale (Mat, L)
[m, n] = size(Mat);
temp = zeros(m, n*L);
for i = 1:m
cur_row =Mat(i,:);
cur_row = cur_row (ones(1,L),:);
temp(i,:) = reshape(cur_row, 1, n*L);
end
y = temp;
end

3টি মন্তব্য