Using two approaches, write the MATLAB code to compute the percentage of concuss
ID: 3804940 • Letter: U
Question
Using two approaches, write the MATLAB code to compute the percentage of concussions overall (i.e., not classified by gender or sport) in the concuss.dat dataset. a. Approach 1: Use a loop to iterate over each data record, incrementing tallies as appropriate. b. Approach 2: Do NOT use a loop. Instead, index into your matrix to define the relevant vectors. Then use a logical mask to identify the subset of interest. For both approaches, use an fprintf statement to produce this output: Overall, 0.074 6% of the athletes sustained concussions. Notes: Be sure to load the data file into memory (once is sufficient). You may NOT use MATLAB's sum function in Approach 1. To produce the % sign in fprintf, use %%. Do NOT hard-code the 0.0746!! Comments are NOT required. As a reminder, here are the first few records (rows) contained in the concuss.dat file. The columns, in order, represent sport, gender, year, concussion and count.Explanation / Answer
result = [];
fid=fopen('concuss.dat');
while i < 10
tline = fgetl(fid);
if ~ischar(tline), break, end
cellData = textscan(tline,'%d %d %d %d %d');
matData = cell2mat(cellData);
result = [result ; matData];
end
fclose(fid);
% Approach one
total = length(result);
sumValue = 0;
for i=1:1:total
if result(i, 4) == 0
continue;
else
sumValue += 1;
end
end
percentage = (sumValue * 100.0) / total;
fprintf("Overall, %f of the athletes sustained concussions. ", percentage);
% Approach two
sumValue = length(findstr(result(:,[4])==1, 1))
percentage = length(result);
fprintf("Overall, %f of the athletes sustained concussions. ", percentage);
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.