The variable countries is a 27×1 column vector with 3 letter abbreviations (ex. 'CAN'). Results is a 27×4 matrix with values ranging from 0-40 ish. Why can't I print them together or row by row?
I want the result to look like this:
Country Gold Silver Bronze
CAN 2 1 3
ITA 3 1 4
… etc.
My code:
Please skip to the bottom of this code for the part I'm having issues with.
function [] = Untitled6()load('olympics.mat')results = zeros(size(countries,1),4);results = compute_medals(gold,silver,bronze,countries);print_country_results(countries,results);endfunction results = compute_medals(gold,silver,bronze,countries);% computes number of gold, silver, bronze medals and
% total medal tally for a given country and a given sport type
% results = zeros(size(countries,1),4);
for i = 1:length(countries) country = countries(i, :); goldCount = 0; silverCount = 0; bronzeCount = 0; for j = 1: length(gold) if country == gold(j, :) goldCount = goldCount+ 1; end if country == silver(j, :) silverCount = silverCount + 1; end if country == bronze(j, :) bronzeCount = bronzeCount + 1; end end results(i,1) = goldCount; results(i,2) = silverCount; results(i,3) = bronzeCount; results(i,4) = goldCount + silverCount + bronzeCount;endresultsend% HERE! Below is the part I'm having issues with! Please Help!
function [ ] = print_country_results(countries,results)% prints formatted results
fprintf('Country Gold Silver Bronze Total\n')for i = 1:length(countries) % = 27
fprintf('%s %d \n', countries(i, :), results (i,:)); endend
Can I print a matrix & string row by row? Please help!
EDIT: We are not allowed to use a table. π
Best Answer