Cell array indexing question
조회 수: 1 (최근 30일)
이전 댓글 표시
Hi All,
I'm trying to have it so that this will loop thorugh and put all the numbers/ characters in for the different places in the fprintf statement. I have it so it will run the name, but after that it won't run the right thing. Any help would be appreciated!
%% Part A
load('QuizGrades.mat')
student_Names={'Student1','Student2','Student3','Student4','Student5','Student6','Student7','Student8','Student9','Student10','Student11','Student12','Student13','Student14'};
student_IDs=[101:114];
grade_Book={student_Names,student_IDs,grades}
cellplot(grade_Book)
%% Part B
mean(grade_Book{3}(3,:))% mean of the quiz grades for student 3
mean(grade_Book{3}(:,5))% mean of student grades on quiz 5
mean(grade_Book{3}())% mean of grades for all quizzes
%% Part C
[M,I]=min(grade_Book{3},[],2)%minimum grade for each student in one vector and the index for the quiz in another
%% Part D
for x=[1:14]
fprintf('<%s>(%d)will drop grade(%s)from grade(%s)',(grade_Book{1}{:}),(grade_Book{2}(:)),(grade_Book{3}{:}),I)
end
I'm trying to have it to say <student name> (student ID) will drop (grade) from quiz (indexed quiz number)
Thank you for your help in advance!
댓글 수: 2
채택된 답변
Stephen23
2021년 2월 22일
%% Part A
S = load('QuizGrades.mat');
grades = S.grades
student_Names = "Student"+(1:14)
student_IDs = 101:114;
%% Part B
mean(grades(3,:))% mean of the quiz grades for student 3
mean(grades(:,5))% mean of student grades on quiz 5
mean(grades)% mean of grades for all quizzes
%% Part C
[M,I] = min(grades,[],2); %minimum grade for each student in one vector and the index for the quiz in another
%% Part D
fmt = '<%s>(%d)will drop grade(%d)from quiz(%d)\n';
for k = 1:14
fprintf(fmt,student_Names(k),student_IDs(k),M(k),I(k))
end
Or avoiding the loop:
tmp = [cellstr(student_Names);num2cell([student_IDs(:),M,I]).'];
fprintf(fmt,tmp{:})
댓글 수: 0
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Whos에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!