MATLAB Answers

Cell array indexing question

34 views (last 30 days)
Brittany Isbister
Brittany Isbister on 20 Feb 2021
Answered: Stephen Cobeldick on 22 Feb 2021
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 Comments
Brittany Isbister
Brittany Isbister on 21 Feb 2021
Hi Stephen,
The 'QuizGrades.mat' is uploaded for you!

Sign in to comment.

Answers (1)

Stephen Cobeldick
Stephen Cobeldick on 22 Feb 2021
%% Part A
S = load('QuizGrades.mat');
grades = S.grades
grades = 14×7
78 88 56 67 85 96 63 45 98 42 79 55 61 74 56 80 45 83 70 51 44 73 42 90 86 82 55 43 98 91 82 56 94 77 72 98 96 59 81 98 68 87 49 81 97 79 73 61 96 99 86 42 49 48 90 47 98 85 66 47 49 75 74 69 63 63 70 55 73 68
student_Names = "Student"+(1:14)
student_Names = 1×14 string array
"Student1" "Student2" "Student3" "Student4" "Student5" "Student6" "Student7" "Student8" "Student9" "Student10" "Student11" "Student12" "Student13" "Student14"
student_IDs = 101:114;
%% Part B
mean(grades(3,:))% mean of the quiz grades for student 3
ans = 61.2857
mean(grades(:,5))% mean of student grades on quiz 5
ans = 71.2857
mean(grades)% mean of grades for all quizzes
ans = 1×7
75.6429 75.9286 66.8571 70.2143 71.2857 73.5714 64.6429
%% 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
<Student1>(101)will drop grade(56)from quiz(3) <Student2>(102)will drop grade(42)from quiz(3) <Student3>(103)will drop grade(44)from quiz(7) <Student4>(104)will drop grade(42)from quiz(2) <Student5>(105)will drop grade(56)from quiz(4) <Student6>(106)will drop grade(59)from quiz(3) <Student7>(107)will drop grade(49)from quiz(1) <Student8>(108)will drop grade(42)from quiz(3) <Student9>(109)will drop grade(47)from quiz(4) <Student10>(110)will drop grade(55)from quiz(5) <Student11>(111)will drop grade(40)from quiz(7) <Student12>(112)will drop grade(48)from quiz(1) <Student13>(113)will drop grade(49)from quiz(7) <Student14>(114)will drop grade(41)from quiz(2)
Or avoiding the loop:
tmp = [cellstr(student_Names);num2cell([student_IDs(:),M,I]).'];
fprintf(fmt,tmp{:})
<Student1>(101)will drop grade(56)from quiz(3) <Student2>(102)will drop grade(42)from quiz(3) <Student3>(103)will drop grade(44)from quiz(7) <Student4>(104)will drop grade(42)from quiz(2) <Student5>(105)will drop grade(56)from quiz(4) <Student6>(106)will drop grade(59)from quiz(3) <Student7>(107)will drop grade(49)from quiz(1) <Student8>(108)will drop grade(42)from quiz(3) <Student9>(109)will drop grade(47)from quiz(4) <Student10>(110)will drop grade(55)from quiz(5) <Student11>(111)will drop grade(40)from quiz(7) <Student12>(112)will drop grade(48)from quiz(1) <Student13>(113)will drop grade(49)from quiz(7) <Student14>(114)will drop grade(41)from quiz(2)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by