×
INTELLIGENT WORK FORUMS
FOR ENGINEERING PROFESSIONALS

Log In

Come Join Us!

Are you an
Engineering professional?
Join Eng-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Eng-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

Help needed with Matlab script (Some may consider basic)

Help needed with Matlab script (Some may consider basic)

Help needed with Matlab script (Some may consider basic)

(OP)
Hi,
Please help me I am in dire need of assistance, with the following problem.

I need to create a schedule such that employees at training sessions can hold a series of head-to-head discussions.
Each trainee is to talk with every other trainee and everyone must be involved in each round of talks, except when there is an odd number of people.

Most training sessions attract 20 people or more. I have to write a Matlab script which prompts for the number of trainees and then prints out the pairings in each round of discussions. It needs to generate schedules for any number (odd and even numbers) of participants. I have been told to use Latin Squares as the base of the script.

Knowledge on Latin Squares can be found here:
http://www.cut-the-knot.com/arithmetic/latin2.html

I know that the question is a bit long winded, but I would be extremly grateful if a suitable matlab solution, or any ideas to this problem could be posted to me within a week.

RE: Help needed with Matlab script (Some may consider basic)

Below is some code to do a round robin.

I was unable to quickly implement a round robin based on the Latin matrix method.  The following code is based on the comments at http://www.magma.ca/~acna/rrsched.htm.

Please check the code with several examples to convince yourself that it is correct.  I've tested it for the two cases indicated.  I was having trouble for an odd number of people.  You can see I kludge a fix in, and added an additional player 'BYE', so there is an even number of people.

You can put the correct names of the people in near the top of the code, and they will be written to a file.

Cheers
J. Vorwald

% Program starts here

function main

% Test 1 - Even number of players

names1 = str2mat('Player 1', 'Player 2', ...
'Player 3', 'Player 4', 'Player 5', 'Player 6');

rounds = roundRobin(names1);
fid = fopen('round2.txt','w');
fprintf(fid,'Test 1: Even number of players\n\n');
printRoundRobin(fid,rounds,names1);

% Test 2 - Odd number of players

names2 = str2mat('Player 1', 'Player 2', ...
'Player 3', 'Player 4', 'Player 5');
rounds = roundRobin(names2);
fprintf(fid,'\nTest 2: Odd number of players\n\n');
printRoundRobin(fid,rounds,names2);

fclose(fid);

function printRoundRobin(fid,rounds,names)

fprintf(fid,'\nRound robin for players:\n');
number_of_people = size(names,1);
for iplayer=1:number_of_people
    fprintf(fid,'\t%s\n',deblank(names(iplayer,:)));
end

[number_of_people, names] = fixNames(names);

fprintf(fid,'\n');

[numberOfRounds, numberOfMatches] = getTourneymentSize(number_of_people);

for iround=1:numberOfRounds
    fprintf(fid,'Round %d\n',iround);
    for imatch=1:numberOfMatches
        player1 = rounds(iround).matches(imatch,1);
        player2 = rounds(iround).matches(imatch,2);
%        fprintf(fid,'\tMatch %d: Player %d plays %d\n',imatch, player1, player2);
        name1 = deblank(names(player1,:));
        name2 = deblank(names(player2,:));
        
        if (strcmp(name1,'BYE'))
            fprintf(fid,'\tMatch %d: Player %s has a bye\n',imatch, name2);
        elseif (strcmp(name2,'BYE'))
            fprintf(fid,'\tMatch %d: Player %s has a bye\n',imatch, name1);
        else
            fprintf(fid,'\tMatch %d: Player %s plays %s\n',imatch, name1, name2);
        end
    end
end


function rounds = roundRobin(names)

% This routine is based on comments at http://www.magma.ca/~acna/rrsched.htm

% Think of a circle, with one person in the middle, and the other people in a circle
% around him.

% In the first round, the person in the middle plays the first person on the circle
% The 2nd match is the two people from both sides of the first person on the circle
% The 3rd match is the next two people from both sides of the first person on the circle
% And so on for the rest of the matches

% In the second round, the person in the middle plays the second person on the circle
% The 2nd match is the two people from both sides of the second person on the circle
% The 3rd match is the next two people from both sides of the second person on the circle
% And so on for the rest of the matches

% In the third round, the person in the middle plays the third person on the circle
% etc

% To accommodate N players (N even), you need a minimum of N-1 rounds. To accommodate
% M players (M odd), you need a minimum of M rounds.

% When there is an odd number of players, add a person named BYE.

[number_of_people, names] = fixNames(names);


outsidePlayers = [1:number_of_people-1];
playerInTheMiddle = number_of_people;

[numberOfRounds, numberOfMatches] = getTourneymentSize(number_of_people);
 
circleOfOutsidePlayers = [outsidePlayers outsidePlayers outsidePlayers];

for iround=1:numberOfRounds

    outsidePlayer = number_of_people - 1 + iround;
    person1 = circleOfOutsidePlayers(outsidePlayer);
    person2 = playerInTheMiddle;
    rounds(iround).matches(1,1) = person1;
    rounds(iround).matches(1,2) = person2;
    
    for imatch = 2:numberOfMatches
        outsidePlayer1 = outsidePlayer - (imatch-1);
        outsidePlayer2 = outsidePlayer + (imatch-1);
        person1 = circleOfOutsidePlayers(outsidePlayer1);
        person2 = circleOfOutsidePlayers(outsidePlayer2);
        rounds(iround).matches(imatch,1) = person1;
        rounds(iround).matches(imatch,2) = person2;
    end
end

function [numberOfRounds, numberOfMatches] = getTourneymentSize(number_of_people)
if (mod(number_of_people,2) == 1)
    numberOfRounds = number_of_people;
    numberOfMatches = (number_of_people - 1) / 2;
else
    numberOfRounds = number_of_people - 1;
    numberOfMatches = number_of_people / 2;
end

function [number_of_people, names] = fixNames(names);
number_of_people=size(names,1);

if (mod(number_of_people,2) == 1)
    names = str2mat(names,'BYE');
    number_of_people=size(names,1);
end


Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Eng-Tips Forums free from inappropriate posts.
The Eng-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Eng-Tips forums is a member-only feature.

Click Here to join Eng-Tips and talk with other members!


Resources