MATLAB 元胞数组 生成随机学生数据并计算平均分、最高分及最大平均分学号
以下是使用 MATLAB 实现上述操作的代码:
% 生成数据
data = {
'班级' '姓名' '学号' '分数';
'A' '张三' '1001' randi([50, 100], 1, 3);
'A' '李四' '1002' randi([50, 100], 1, 3);
'A' '王五' '1003' randi([50, 100], 1, 3);
'B' '赵六' '2001' randi([50, 100], 1, 3);
'B' '钱七' '2002' randi([50, 100], 1, 3);
'B' '孙八' '2003' randi([50, 100], 1, 3);
'C' '周九' '3001' randi([50, 100], 1, 3);
'C' '吴十' '3002' randi([50, 100], 1, 3);
'C' '郑十一' '3003' randi([50, 100], 1, 3);
};
% 计算每名学生的平均分
scores = cell2mat(data(2:end, 4:end));
averages = mean(scores, 2);
% 找出最高分及最大平均分学号
[maxScore, maxIdx] = max(scores(:));
[maxAvg, maxAvgIdx] = max(averages);
% 显示结果
disp('每名学生的平均分:');
disp(averages);
disp(['最高分:' num2str(maxScore) ', 学号:' data{maxIdx+1, 3}]);
disp(['最大平均分:' num2str(maxAvg) ', 学号:' data{maxAvgIdx+1, 3}]);
运行以上代码将输出每名学生的平均分、最高分及其对应学号,以及最大平均分及其对应学号。
原文地址: https://www.cveoy.top/t/topic/m5N 著作权归作者所有。请勿转载和采集!