Advertisement
Nurio

General tallier - Set up to be used for Pony episode ranks

Mar 25th, 2012
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 7.26 KB | None | 0 0
  1. % Tallies a ranking poll where one ranks a list of options from e.g. best to worst
  2.  
  3. clear all
  4. clc
  5.  
  6. % -- Setting up poll data -- (Has to be set up manually here)
  7.  
  8. % List of poll options should be filled in here like:
  9. % string(n) = struct('name','#NAME#');
  10. % where n is the poll option number/id running up from 1
  11. % and #NAME# is the name/title of the respective poll option
  12. string(1) = struct('name','Return of Harmony, part 1');
  13. string(2) = struct('name','Return of Harmony, part 2');
  14. string(3) = struct('name','Lesson Zero');
  15. string(4) = struct('name','Luna Eclipsed');
  16. string(5) = struct('name','Sisterhooves Social');
  17. string(6) = struct('name','The Cutie Pox');
  18. string(7) = struct('name','May the Best Pet Win!');
  19. string(8) = struct('name','Mysterious Mare Do Well');
  20. string(9) = struct('name','Sweet and Elite');
  21. string(10) = struct('name','Secret of My Excess');
  22. string(11) = struct('name','Family Appreciation Day');
  23. string(12) = struct('name','Baby Cakes');
  24. string(13) = struct('name',['Hearth',char(39),'s Warming Eve']);
  25. string(14) = struct('name','The Last Roundup');
  26. string(15) = struct('name','The Super Speedy Cider Squeezy 6000');
  27. string(16) = struct('name','Read It and Weep');
  28. string(17) = struct('name','Hearts and Hooves Day');
  29. string(18) = struct('name','A Friend in Deed');
  30. string(19) = struct('name','Putting Your Hoof Down');
  31. string(20) = struct('name',['It',char(39),'s About Time']);
  32. string(21) = struct('name','Dragon Quest');
  33. string(22) = struct('name','Hurricane Fluttershy');
  34. % List of poll options ends here
  35. max_data = size(string);
  36. max_data = max_data(2); % max_data is simply to number of poll options
  37.  
  38. % -- Loading process -- (Either loads existing data from external file or starts from scratch)
  39.  
  40. if  str(input('Load previous data? ','s'),'Yes') % Simple structure which asks to load data from scores.mat
  41.     disp('Loading previous data...')
  42.     load('scores.mat'); % Loads the score data and the number of processed rank lists from an external file
  43. else
  44.     disp('Starting from scratch...')
  45.     scores = zeros(max_data,1); % scores is the amount of points a poll option got (scores is in order of poll option ID 1 to highest poll option ID)
  46.     rank_lists = 0; % rank_lists is simply the number of ranking lists that has been processed in all data
  47. end
  48.  
  49. % -- Ranking process -- (The only real interactive part. Asks for poll option IDs in order of rank and creates a rank list based on that)
  50.  
  51. abort = 0; % abort and error are checks whether the ranking process has been aborted (by user or error)
  52. error = 0;
  53. while 1 % Keeps going infinitely until aborted by user or error
  54.     disp(' ')
  55.     disp('-- New rank list --')
  56.     disp(' ')
  57.     ranks = zeros(max_data,1); % ranks is the order in which the poll options are ranked, from highest to lowest
  58.     for e = 1:max_data % Goes through all poll options one by one
  59.         a = input(['Rank ',num2str(e),': ']); % Asks which poll option ID ranked e'th place
  60.         if a == 0 % Answering 0 aborts the ranking and scoring process and goes to the displaying process
  61.             abort = 1;
  62.             break
  63.         end
  64.         ranks(e) = a; % Applies the poll option ID to e'th rank
  65.     end
  66.     if abort == 1
  67.         break
  68.     end
  69.    
  70.     % -- Checking for errors -- (Simply checks for simple errors in the rank list)
  71.    
  72.     for e = 1:max_data % Goes through everything in the *rank list* one by one
  73.         if ranks(e) < 1 % If a poll option ID in the rank list is less than 1, it aborts and returns an error
  74.             abort = 1;
  75.             error = 1;
  76.             disp(['Error: A data ID lower than 1 (',num2str(ranks(e)),') has been ranked!'])
  77.             disp('Scores not saved!')
  78.             break
  79.         elseif ranks(e) > max_data % If a poll option ID in the rank list is more than the highest poll option ID, it aborts and returns an error
  80.             abort = 1;
  81.             error = 1;
  82.             disp(['Error: A data ID (',num2str(ranks(e)),') higher than the max number of data (',num2str(max_data),') has been ranked!'])
  83.             disp('Scores not saved!')
  84.             break
  85.         elseif e ~= max_data
  86.             for f = (e+1):max_data
  87.                 if ranks(e) == ranks(f) % Compares every poll option ID in the rank list with each other and aborts and returns an error if there's a double
  88.                     abort = 1;
  89.                     error = 1;
  90.                     disp(['Error: A data ID (',num2str(ranks(e)),') received 2 or more ranks!'])
  91.                     disp('Scores not saved!')
  92.                     break
  93.                 end
  94.                 if abort == 1
  95.                     break
  96.                 end
  97.             end
  98.         end
  99.         if abort == 1
  100.             break
  101.         end
  102.     end
  103.     if abort == 1
  104.         break
  105.     end
  106.    
  107.     % -- Scoring process -- (If there's no problem with the rank list, creates/updates the score list)
  108.    
  109.     for e = 1:max_data
  110.         scores(ranks(e)) = scores(ranks(e)) + (max_data - e + 1); % The e'th rank (i.e. the e'th part in the rank list) gets added: max points - e + 1
  111.     end
  112.     rank_lists = rank_lists + 1; % 1 rank list has been processed in the data
  113.     disp(' ')
  114.     disp('Scores temporarily saved')
  115. end
  116. disp(' ')
  117.  
  118. % -- Displaying process -- (Displays the scores the individual poll options received and also displays them in order of their ranks)
  119.  
  120. if error == 0 % Only does this when the program returned no errors
  121.     disp('Scores: ')
  122.     for e = 1:max_data
  123.         disp([num2str(e),' - ',string(e).name,': ',num2str(scores(e))]) % Shows all poll options from 1 to highest and gives their score
  124.     end
  125.     disp(' ') % To display the total ranks, we need to restructure
  126.     temp1 = zeros(max_data,2);
  127.     for e = 1:max_data
  128.         temp1(e,1:2) = [scores(e),e]; % First creates a two-column matrix of the scores and their corresponding poll option ID
  129.     end
  130.     temp2 = sortrows(temp1); % Then sorts the matrix from lowest score to highest score
  131.     output = zeros(max_data,2);
  132.     for e = 1:max_data
  133.         output(e,1:2) = temp2((max_data - e + 1),1:2); % Simply reverses the ordering, making it from highest to lowest score, calls this matrix output
  134.     end
  135.     disp('Ranks:')
  136.     for e = 1:max_data
  137.         avg_points = (output(e,1) / rank_lists) % Computes the average number of points the e'th rank received
  138.         avg_rank = max_data - avg_points + 1; % Computes the average rank from the average number of points
  139.         disp([num2str(e,'%d'),' - ',string(output(e,2)).name,': ',num2str(avg_rank,'%.2f'),' (',num2str(output(e,1)),')']) % Displays the rank from 1 to lowest and the corresponding poll option. Then it gives the average rank and the number of points it received.
  140.     end
  141.     disp(' ')
  142.     if rank_lists == 1
  143.         disp(['Data measured over ',num2str(rank_lists),' rank list']) % Shows the number of rank lists processed in this data (for 1 rank list)
  144.     else
  145.         disp(['Data measured over ',num2str(rank_lists),' rank lists']) % Shows the number of rank lists processed in this data (for more than 1 rank list)
  146.     end
  147.     disp(' ')
  148.    
  149.     % -- Saving process -- (Saves data to an external file)
  150.    
  151.     save('scores.mat','scores','rank_lists'); % Saves the score data and the number of processed rank lists to an external file
  152.     disp('New data permanently saved!')
  153. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement