MATLAB HELP PLEASE LEAVE COMMENTS AND SHOW ALL STEPS Building a Cell Array Write
ID: 3871282 • Letter: M
Question
MATLAB HELP PLEASE LEAVE COMMENTS AND SHOW ALL STEPS
Building a Cell Array Write a function [studentCell] =classCellArray(studentNames, studentIDs, studentGrades) that accepts a 4 x 3 char array of names (studentNames), a 4 x 6 char array of student IDs (studentIDs), and a 4 x 3 double array of grades (studentGrades). The function should return a 4 x 4 cell array (studentCell) with the data in studentNames occupying the all rows of the first column of the cell array studentCell, the data in studentIDs occupying the all rows of the second column of the cell array studentCell, the data in studentGrades occupying the all rows of the third column of the cell array studentCell, and the fourth column containing the mean of the three grades for each student. i.e. studentCell(1,4) contains the mean of the data in studentGrades(1,:); studentCell(2,4) contains the mean of the data in studentGrades(2,:);etc. A table with sample data is provided below, but the function should work for ANY variable inputs that follow the format specified above.
Be sure to verify that your function can handle the same inputs and produce the same outputs as the test cases below. Check both the class and size!
Table 1: Sample Data for classCellArray.m
Name Student ID Lab
Jon A10293 [70, 81, 90]
Tom B10498 [91, 10, 87]
Rey C10201 [64, 100, 77]
Kip D19532 [100, 99, 77]
Explanation / Answer
The following code will work for your sample data. The second code will work for any size of data that follow the same formate.
%This function will accept data as asked in your question
function [studentCell] = classCellArray(studentNames, studentIDs, studentGrades)
studentCell=cell(4,4); % Define studentCell of size 4x4
for i=1:4 % for each student from 1 to 4
studentCell{i,1}=studentNames(i,:); % copy name of ith student in first cell of ith row
studentCell{i,2}=studentIDs(i,:);% copy studentID of ith student in first cell of ith row
studentCell{i,3}=studentGrades(i,:); % copy grade of ith student in first cell of ith row
studentCell{i,4}=mean(studentGrades(i,:));% the mean() function claculate the average grade of ith student % and put in first cell of ith row
end
Second Code:
%This function will accept data as asked in your question for 'm' many
%students
function [studentCell] = classCellArray(studentNames, studentIDs, studentGrades)
[m,n]=size(studentNames);
studentCell=cell(m,4); % Define studentCell of size 4x4
for i=1:m % for each student from 1 to 4
studentCell{i,1}=studentNames(i,:); % copy name of ith student in first cell of ith row
studentCell{i,2}=studentIDs(i,:);% copy studentID of ith student in first cell of ith row
studentCell{i,3}=studentGrades(i,:); % copy grade of ith student in first cell of ith row
studentCell{i,4}=mean(studentGrades(i,:));% the mean() function claculate the average grade of ith student %and put in first cell of ith row
end
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.