Using two approaches, write the MATLAB code to compute the percentage of concuss
ID: 3604504 • 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. 2. Approach 1: Use a loop to iterate over each data record, incrementing tallies as appropriate. 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. a. b. For both approaches, use an fprintf statement to produce this output: Overall, 0.0746% of the athletes sustained concussions Notes: 1) Be sure to load the data file into memory (once is sufficient) 2) You may NOT use MATLAB's sum function in Approach 1 3) To produce the % sign in fprintf, use %%. 4) Do NOT hard-code the 0.0746!! 5) 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. 1 1997024930 1 1997 1 51 1 1 1998022887 1 1998 1 47Explanation / Answer
%matlab code
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 1
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 2
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.