某班32 位同学某学期三门课程的期末成绩保存于numdata1.xlsx 中,用Matlab根据要求完成下列操作(3)基本数据分析: 统计各门课程的平均分; 分别统计各门课程的及格人数和及格率; 绘制各分数段人数占比图;
时间: 2024-03-09 18:47:00 浏览: 178
以下是Matlab代码,可以实现基本数据分析,包括统计各门课程的平均分,分别统计各门课程的及格人数和及格率,以及绘制各分数段人数占比图:
```Matlab
% 读取Excel文件中的数据
data = readtable('numdata1.xlsx');
% 统计各门课程的平均分
mean_chinese = mean(data.Chinese);
mean_math = mean(data.Math);
mean_english = mean(data.English);
fprintf('The average score of Chinese is %.2f\n', mean_chinese);
fprintf('The average score of Math is %.2f\n', mean_math);
fprintf('The average score of English is %.2f\n', mean_english);
% 分别统计各门课程的及格人数和及格率
pass_chinese = sum(data.Chinese >= 60);
pass_math = sum(data.Math >= 60);
pass_english = sum(data.English >= 60);
pass_rate_chinese = pass_chinese / length(data.Chinese);
pass_rate_math = pass_math / length(data.Math);
pass_rate_english = pass_english / length(data.English);
fprintf('The number of students passed in Chinese is %d, and the pass rate is %.2f\n', pass_chinese, pass_rate_chinese);
fprintf('The number of students passed in Math is %d, and the pass rate is %.2f\n', pass_math, pass_rate_math);
fprintf('The number of students passed in English is %d, and the pass rate is %.2f\n', pass_english, pass_rate_english);
% 绘制各分数段人数占比图
edges = 0:10:100;
chinese_counts = histcounts(data.Chinese, edges);
math_counts = histcounts(data.Math, edges);
english_counts = histcounts(data.English, edges);
total_counts = chinese_counts + math_counts + english_counts;
chinese_ratio = chinese_counts / length(data.Chinese);
math_ratio = math_counts / length(data.Math);
english_ratio = english_counts / length(data.English);
total_ratio = total_counts / length(data.Chinese);
figure;
bar(edges(1:end-1), [chinese_ratio' math_ratio' english_ratio' total_ratio'], 'stacked');
xlabel('Score');
ylabel('Ratio');
legend('Chinese', 'Math', 'English', 'Total');
```
以上代码中,`readtable`函数用于读取Excel文件中的数据,然后分别统计各门课程的平均分和及格人数及及格率。接着使用`histcounts`函数统计各分数段人数占比,最后使用`bar`函数绘制各分数段人数占比图。
阅读全文