Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- % Tallies a ranking poll where one ranks a list of options from e.g. best to worst
- clear all
- clc
- % -- Setting up poll data -- (Has to be set up manually here)
- % List of poll options should be filled in here like:
- % string(n) = struct('name','#NAME#');
- % where n is the poll option number/id running up from 1
- % and #NAME# is the name/title of the respective poll option
- string(1) = struct('name','Return of Harmony, part 1');
- string(2) = struct('name','Return of Harmony, part 2');
- string(3) = struct('name','Lesson Zero');
- string(4) = struct('name','Luna Eclipsed');
- string(5) = struct('name','Sisterhooves Social');
- string(6) = struct('name','The Cutie Pox');
- string(7) = struct('name','May the Best Pet Win!');
- string(8) = struct('name','Mysterious Mare Do Well');
- string(9) = struct('name','Sweet and Elite');
- string(10) = struct('name','Secret of My Excess');
- string(11) = struct('name','Family Appreciation Day');
- string(12) = struct('name','Baby Cakes');
- string(13) = struct('name',['Hearth',char(39),'s Warming Eve']);
- string(14) = struct('name','The Last Roundup');
- string(15) = struct('name','The Super Speedy Cider Squeezy 6000');
- string(16) = struct('name','Read It and Weep');
- string(17) = struct('name','Hearts and Hooves Day');
- string(18) = struct('name','A Friend in Deed');
- string(19) = struct('name','Putting Your Hoof Down');
- string(20) = struct('name',['It',char(39),'s About Time']);
- string(21) = struct('name','Dragon Quest');
- string(22) = struct('name','Hurricane Fluttershy');
- % List of poll options ends here
- max_data = size(string);
- max_data = max_data(2); % max_data is simply to number of poll options
- % -- Loading process -- (Either loads existing data from external file or starts from scratch)
- if str(input('Load previous data? ','s'),'Yes') % Simple structure which asks to load data from scores.mat
- disp('Loading previous data...')
- load('scores.mat'); % Loads the score data and the number of processed rank lists from an external file
- else
- disp('Starting from scratch...')
- 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)
- rank_lists = 0; % rank_lists is simply the number of ranking lists that has been processed in all data
- end
- % -- Ranking process -- (The only real interactive part. Asks for poll option IDs in order of rank and creates a rank list based on that)
- abort = 0; % abort and error are checks whether the ranking process has been aborted (by user or error)
- error = 0;
- while 1 % Keeps going infinitely until aborted by user or error
- disp(' ')
- disp('-- New rank list --')
- disp(' ')
- ranks = zeros(max_data,1); % ranks is the order in which the poll options are ranked, from highest to lowest
- for e = 1:max_data % Goes through all poll options one by one
- a = input(['Rank ',num2str(e),': ']); % Asks which poll option ID ranked e'th place
- if a == 0 % Answering 0 aborts the ranking and scoring process and goes to the displaying process
- abort = 1;
- break
- end
- ranks(e) = a; % Applies the poll option ID to e'th rank
- end
- if abort == 1
- break
- end
- % -- Checking for errors -- (Simply checks for simple errors in the rank list)
- for e = 1:max_data % Goes through everything in the *rank list* one by one
- if ranks(e) < 1 % If a poll option ID in the rank list is less than 1, it aborts and returns an error
- abort = 1;
- error = 1;
- disp(['Error: A data ID lower than 1 (',num2str(ranks(e)),') has been ranked!'])
- disp('Scores not saved!')
- break
- 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
- abort = 1;
- error = 1;
- disp(['Error: A data ID (',num2str(ranks(e)),') higher than the max number of data (',num2str(max_data),') has been ranked!'])
- disp('Scores not saved!')
- break
- elseif e ~= max_data
- for f = (e+1):max_data
- 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
- abort = 1;
- error = 1;
- disp(['Error: A data ID (',num2str(ranks(e)),') received 2 or more ranks!'])
- disp('Scores not saved!')
- break
- end
- if abort == 1
- break
- end
- end
- end
- if abort == 1
- break
- end
- end
- if abort == 1
- break
- end
- % -- Scoring process -- (If there's no problem with the rank list, creates/updates the score list)
- for e = 1:max_data
- 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
- end
- rank_lists = rank_lists + 1; % 1 rank list has been processed in the data
- disp(' ')
- disp('Scores temporarily saved')
- end
- disp(' ')
- % -- Displaying process -- (Displays the scores the individual poll options received and also displays them in order of their ranks)
- if error == 0 % Only does this when the program returned no errors
- disp('Scores: ')
- for e = 1:max_data
- disp([num2str(e),' - ',string(e).name,': ',num2str(scores(e))]) % Shows all poll options from 1 to highest and gives their score
- end
- disp(' ') % To display the total ranks, we need to restructure
- temp1 = zeros(max_data,2);
- for e = 1:max_data
- temp1(e,1:2) = [scores(e),e]; % First creates a two-column matrix of the scores and their corresponding poll option ID
- end
- temp2 = sortrows(temp1); % Then sorts the matrix from lowest score to highest score
- output = zeros(max_data,2);
- for e = 1:max_data
- 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
- end
- disp('Ranks:')
- for e = 1:max_data
- avg_points = (output(e,1) / rank_lists) % Computes the average number of points the e'th rank received
- avg_rank = max_data - avg_points + 1; % Computes the average rank from the average number of points
- 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.
- end
- disp(' ')
- if rank_lists == 1
- disp(['Data measured over ',num2str(rank_lists),' rank list']) % Shows the number of rank lists processed in this data (for 1 rank list)
- else
- 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)
- end
- disp(' ')
- % -- Saving process -- (Saves data to an external file)
- save('scores.mat','scores','rank_lists'); % Saves the score data and the number of processed rank lists to an external file
- disp('New data permanently saved!')
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement