{"id":147,"date":"2008-07-23T09:16:02","date_gmt":"2008-07-23T14:16:02","guid":{"rendered":"https:\/\/blogs.mathworks.com\/loren\/2008\/07\/23\/using-matlab-to-grade\/"},"modified":"2016-08-02T14:39:29","modified_gmt":"2016-08-02T19:39:29","slug":"using-matlab-to-grade","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/loren\/2008\/07\/23\/using-matlab-to-grade\/","title":{"rendered":"Using MATLAB  to Grade"},"content":{"rendered":"<div xmlns:mwsh=\"https:\/\/www.mathworks.com\/namespace\/mcode\/v1\/syntaxhighlight.dtd\" class=\"content\">\r\n   <introduction>\r\n      <p>Educators use MATLAB a lot.  In addition to using MATLAB for research, many professors and instructors use MATLAB for teaching,\r\n         including demonstrating and explaining concepts, creating class notes, and creating and collecting homework assignments and\r\n         exams.  Today I will show how you might use a <a href=\"https:\/\/www.mathworks.com\/help\/stats\/dataset.html\"><tt>dataset<\/tt> array<\/a> from <a href=\"https:\/\/www.mathworks.com\/products\/statistics\/\">Statistic Toolbox<\/a> to grade a set of student results that are already recorded.\r\n      <\/p>\r\n   <\/introduction>\r\n   <h3>Contents<\/h3>\r\n   <div>\r\n      <ul>\r\n         <li><a href=\"#1\">What is a dataset Array?<\/a><\/li>\r\n         <li><a href=\"#4\">Why Use a dataset Array?<\/a><\/li>\r\n         <li><a href=\"#5\">Creating a dataset<\/a><\/li>\r\n         <li><a href=\"#9\">Merge All into a Single dataset<\/a><\/li>\r\n         <li><a href=\"#10\">Transform the Underlying Data<\/a><\/li>\r\n         <li><a href=\"#14\">Gathering Information Per Question<\/a><\/li>\r\n         <li><a href=\"#19\">Which Questions are Hard?<\/a><\/li>\r\n         <li><a href=\"#20\">Score the Assignments for Each Student<\/a><\/li>\r\n         <li><a href=\"#23\">How Can You See Using a dataset Array?<\/a><\/li>\r\n      <\/ul>\r\n   <\/div>\r\n   <h3>What is a dataset Array?<a name=\"1\"><\/a><\/h3>\r\n   <p>A <tt>dataset<\/tt> array is basically two-dimensional array where each column holds data represented in a single data type, but the columns\r\n      together may comprise many different data types.\r\n   <\/p>\r\n   <p>A cell array can do this, but does not enforce the idea that all the elements in a given column must have the same type. \r\n      To have names with a cell array, you either need to carry around an extra array, or have a special row or column to contain\r\n      the label.\r\n   <\/p>\r\n   <p>A scalar structure similarly can perform this service but but does not enforce the idea that each field must hold the same\r\n      number of rows. Additionally, a <tt>dataset<\/tt> array can have labels that allow you to reference data not just by numeric or logical indexing, but by names as well.\r\n   <\/p>\r\n   <h3>Why Use a dataset Array?<a name=\"4\"><\/a><\/h3>\r\n   <p>You might want to use a <tt>dataset<\/tt> array if the data you have is natural to think of as collection of different but related entities.  The relationships between\r\n      the collections are more constrained (in this case, 1:1) than the flexibility afforded by cells or structs. The example I'm\r\n      showing here, grading an assignment, shows some of the simpler ways in which you might use a <tt>dataset<\/tt> array.\r\n   <\/p>\r\n   <h3>Creating a dataset<a name=\"5\"><\/a><\/h3>\r\n   <p>I have an assignment with 5 questions, 2 T\/F and 3 multiple choice (a:d). The questions have extraordinarily imaginative names.<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">qnames = {<span style=\"color: #A020F0\">'Q1'<\/span> <span style=\"color: #A020F0\">'Q2'<\/span> <span style=\"color: #A020F0\">'Q3'<\/span> <span style=\"color: #A020F0\">'Q4'<\/span> <span style=\"color: #A020F0\">'Q5'<\/span>};<\/pre><p>I have collected the students' results in a sheet of a spreadsheet labeled \"students\" and the truth in a sheet labeled \"truth\".\r\n       I can read each of these directly into a <tt>dataset<\/tt> array so the results are available for analysis.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">answers = dataset(<span style=\"color: #A020F0\">'xlsfile'<\/span>,<span style=\"color: #A020F0\">'class answers.xls'<\/span>,<span style=\"color: #0000FF\">...<\/span>\r\n    <span style=\"color: #A020F0\">'sheet'<\/span>,<span style=\"color: #A020F0\">'students'<\/span>,<span style=\"color: #A020F0\">'ReadObsNames'<\/span>,true,<span style=\"color: #0000FF\">...<\/span>\r\n    <span style=\"color: #A020F0\">'ReadVarNames'<\/span>,false,<span style=\"color: #A020F0\">'VarNames'<\/span>,qnames)<\/pre><pre style=\"font-style:oblique\">answers = \r\n                   Q1    Q2         Q3    Q4         Q5     \r\n    Chris          0     'a'        0     'c'        'b'    \r\n    Christine      0     'b'        0     'c'        'a'    \r\n    Christopher    1     'a'        0     'c'        'a'    \r\n    Kris           1     'a'        1     'b'        'c'    \r\n    Kristen        1     'a'        0     'd'        'a'    \r\n<\/pre><p>As you can see, all my students have similar names. In fact, I believe the most common root name for employees at MathWorks\r\n      is this same root.  Each row represents the results for a given student, and the columns represent the results for a given\r\n      question.\r\n   <\/p>\r\n   <p>Here are the \"real\" answers.<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">truth = dataset(<span style=\"color: #A020F0\">'xlsfile'<\/span>,<span style=\"color: #A020F0\">'class answers.xls'<\/span>,<span style=\"color: #0000FF\">...<\/span>\r\n    <span style=\"color: #A020F0\">'sheet'<\/span>,<span style=\"color: #A020F0\">'truth'<\/span>,<span style=\"color: #A020F0\">'ReadObsNames'<\/span>,true,<span style=\"color: #0000FF\">...<\/span>\r\n    <span style=\"color: #A020F0\">'ReadVarNames'<\/span>,false,<span style=\"color: #A020F0\">'VarNames'<\/span>,qnames)<\/pre><pre style=\"font-style:oblique\">truth = \r\n               Q1    Q2         Q3    Q4         Q5     \r\n    Answers    1     'a'        0     'c'        'a'    \r\n<\/pre><h3>Merge All into a Single dataset<a name=\"9\"><\/a><\/h3>\r\n   <p>I am placing the answer key in the top row of my array so I have the truth handy for comparison later.<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">alldata = [truth; answers]<\/pre><pre style=\"font-style:oblique\">alldata = \r\n                   Q1    Q2         Q3    Q4         Q5     \r\n    Answers        1     'a'        0     'c'        'a'    \r\n    Chris          0     'a'        0     'c'        'b'    \r\n    Christine      0     'b'        0     'c'        'a'    \r\n    Christopher    1     'a'        0     'c'        'a'    \r\n    Kris           1     'a'        1     'b'        'c'    \r\n    Kristen        1     'a'        0     'd'        'a'    \r\n<\/pre><h3>Transform the Underlying Data<a name=\"10\"><\/a><\/h3>\r\n   <p>As you can see, the answers show up as a mixture of string values, 1s, and 0s.  If fact, I prefer to think of the 1s and 0s\r\n      as true and false.  Also, the string values are the results from multiple choice questions - where the answers are limited\r\n      to values <b>a<\/b> through <b>d<\/b>. I would prefer to see the results reflected in the way I want to think about them.  So I now transform the data, column\r\n      by column.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\"><span style=\"color: #0000FF\">for<\/span> q = qnames\r\n    <span style=\"color: #228B22\">% Get all the values for a question.<\/span>\r\n    vals = alldata.(q{1});\r\n    <span style=\"color: #228B22\">% Change numeric values to logical.<\/span>\r\n    <span style=\"color: #0000FF\">if<\/span> isnumeric(vals)\r\n        alldata.(q{1}) = logical(vals);\r\n    <span style=\"color: #0000FF\">else<\/span>\r\n    <span style=\"color: #228B22\">% Change non-numeric values to the collection 'a':'d'<\/span>\r\n        alldata.(q{1}) = nominal(vals,[],{<span style=\"color: #A020F0\">'a'<\/span>,<span style=\"color: #A020F0\">'b'<\/span>,<span style=\"color: #A020F0\">'c'<\/span>,<span style=\"color: #A020F0\">'d'<\/span>});\r\n    <span style=\"color: #0000FF\">end<\/span>\r\n<span style=\"color: #0000FF\">end<\/span><\/pre><p>Here's a summary of the transformed data.<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">summary(alldata)<\/pre><pre style=\"font-style:oblique\">Q1: [6x1 logical]\r\n     true      false \r\n        4          2 \r\nQ2: [6x1 nominal]\r\n     a      b      c      d \r\n     5      1      0      0 \r\nQ3: [6x1 logical]\r\n     true      false \r\n        1          5 \r\nQ4: [6x1 nominal]\r\n     a      b      c      d \r\n     0      1      4      1 \r\nQ5: [6x1 nominal]\r\n     a      b      c      d \r\n     4      1      1      0 \r\n<\/pre><p>It shows me, by column, the size and type of the data and, in this case, a count of how many results there are for each possible\r\n      value.\r\n   <\/p>\r\n   <p>You may have noticed that I converted the multiple choice columns to a type called <a href=\"https:\/\/www.mathworks.com\/help\/stats\/nominal.html\"><tt>nominal<\/tt><\/a>. The idea of a <tt>nominal<\/tt> array is constrain the values in the array to a specific collection of values.  If all the acceptable values for the array\r\n      are represented in the data, you often don't need more than the first input.  Since some of my columns did not include all\r\n      possible values a:d, I supplied these as the levels. Since they are strings, I choose to use them as the labels for the data\r\n      as well as the values.\r\n   <\/p>\r\n   <h3>Gathering Information Per Question<a name=\"14\"><\/a><\/h3>\r\n   <p>I am now poised to gather information either by question or by student. Let's first look at the answers for questions 1 and\r\n      4.  This is another <tt>dataset<\/tt> array.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">q14Truth = alldata(<span style=\"color: #A020F0\">'Answers'<\/span>,{<span style=\"color: #A020F0\">'Q1'<\/span> <span style=\"color: #A020F0\">'Q4'<\/span>})<\/pre><pre style=\"font-style:oblique\">q14Truth = \r\n               Q1       Q4\r\n    Answers    true     c \r\n<\/pre><p>To get a single answer, I have another option.  This returns a <tt>nominal<\/tt> value.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">q4ans = alldata.Q4(<span style=\"color: #A020F0\">'Answers'<\/span>)<\/pre><pre style=\"font-style:oblique\">q4ans = \r\n     c \r\n\r\n<\/pre><p>I can get all the students' answers to a particular question.  I get a <tt>nominal<\/tt> vector in return here since the <tt>Q4<\/tt> column contains the answers to a multiple choice question.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">q4all = alldata.Q4(2:end)<\/pre><pre style=\"font-style:oblique\">q4all = \r\n     c \r\n     c \r\n     c \r\n     b \r\n     d \r\n\r\n<\/pre><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">whos <span style=\"color: #A020F0\">q4*<\/span><\/pre><pre style=\"font-style:oblique\">  Name       Size            Bytes  Class      Attributes\r\n\r\n  q4all      5x1               314  nominal              \r\n  q4ans      1x1               306  nominal              \r\n\r\n<\/pre><p>And I can also find out all answers for one student, resulting in another <tt>dataset<\/tt> array.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">ChrisAnswers = alldata(<span style=\"color: #A020F0\">'Chris'<\/span>,:)<\/pre><pre style=\"font-style:oblique\">ChrisAnswers = \r\n             Q1       Q2    Q3       Q4    Q5\r\n    Chris    false    a     false    c     b \r\n<\/pre><h3>Which Questions are Hard?<a name=\"19\"><\/a><\/h3>\r\n   <p>Suppose I want to find out which questions are hardest for this set of students.  I can use the <tt>datasetfun<\/tt> function, similar to <tt>cellfun<\/tt> and <tt>arrayfun<\/tt>, to apply a function to each variable in the data. First I need to find out which questions students got right and wrong\r\n      so I can compare their answers to the truth (row 1).\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">f = @(x) 100*sum(x(1)==x(2:end))\/(size(alldata,1)-1)\r\npercentRight = datasetfun(f,alldata)<\/pre><pre style=\"font-style:oblique\">f = \r\n    @(x)100*sum(x(1)==x(2:end))\/(size(alldata,1)-1)\r\npercentRight =\r\n    60    80    80    60    60\r\n<\/pre><h3>Score the Assignments for Each Student<a name=\"20\"><\/a><\/h3>\r\n   <p>use datasetfun by comparing all elements in a column for students (i.e., 2:end) with first element of that column, the right\r\n      answer.  I make an effort to label the rows with the students' names here.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">f = @(x) x(1)==x(2:end)\r\nrightWrong = datasetfun(f,alldata,<span style=\"color: #A020F0\">'DatasetOutput'<\/span>,true,<span style=\"color: #0000FF\">...<\/span>\r\n    <span style=\"color: #A020F0\">'ObsNames'<\/span>,alldata.Properties.ObsNames(2:end))<\/pre><pre style=\"font-style:oblique\">f = \r\n    @(x)x(1)==x(2:end)\r\nrightWrong = \r\n                   Q1       Q2       Q3       Q4       Q5   \r\n    Chris          false    true     true     true     false\r\n    Christine      false    false    true     true     true \r\n    Christopher    true     true     true     true     true \r\n    Kris           true     true     false    false    false\r\n    Kristen        true     true     true     false    true \r\n<\/pre><p>Next I sum across the rows to get scores for each student and add the score as the last column to the <tt>dataset<\/tt>.  Remember: the first score is for the answer key so I set that score to 100.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">alldata.grade = [100; <span style=\"color: #0000FF\">...<\/span>\r\n    100*sum(double(rightWrong),2)\/size(alldata,2)]<\/pre><pre style=\"font-style:oblique\">alldata = \r\n                   Q1       Q2    Q3       Q4    Q5    grade\r\n    Answers        true     a     false    c     a     100  \r\n    Chris          false    a     false    c     b      60  \r\n    Christine      false    b     false    c     a      60  \r\n    Christopher    true     a     false    c     a     100  \r\n    Kris           true     a     true     b     c      40  \r\n    Kristen        true     a     false    d     a      80  \r\n<\/pre><p>Notice that <tt>alldata<\/tt> now contains a new column with numeric values in addition to the <tt>nominal<\/tt> and <tt>logical<\/tt> ones.\r\n   <\/p>\r\n   <h3>How Can You See Using a dataset Array?<a name=\"23\"><\/a><\/h3>\r\n   <p>Can you see applications in which you'd be able to take advantage of a working with your data as a <tt>dataset<\/tt> array?  Let me know <a href=\"https:\/\/blogs.mathworks.com\/loren\/?p=147#respond\">here<\/a>.\r\n   <\/p><script language=\"JavaScript\">\r\n<!--\r\n\r\n    function grabCode_404c55f1d72c40348e23c8808a3f7eba() {\r\n        \/\/ Remember the title so we can use it in the new page\r\n        title = document.title;\r\n\r\n        \/\/ Break up these strings so that their presence\r\n        \/\/ in the Javascript doesn't mess up the search for\r\n        \/\/ the MATLAB code.\r\n        t1='404c55f1d72c40348e23c8808a3f7eba ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' 404c55f1d72c40348e23c8808a3f7eba';\r\n    \r\n        b=document.getElementsByTagName('body')[0];\r\n        i1=b.innerHTML.indexOf(t1)+t1.length;\r\n        i2=b.innerHTML.indexOf(t2);\r\n \r\n        code_string = b.innerHTML.substring(i1, i2);\r\n        code_string = code_string.replace(\/REPLACE_WITH_DASH_DASH\/g,'--');\r\n\r\n        \/\/ Use \/x3C\/g instead of the less-than character to avoid errors \r\n        \/\/ in the XML parser.\r\n        \/\/ Use '\\x26#60;' instead of '<' so that the XML parser\r\n        \/\/ doesn't go ahead and substitute the less-than character. \r\n        code_string = code_string.replace(\/\\x3C\/g, '\\x26#60;');\r\n\r\n        author = 'Loren Shure';\r\n        copyright = 'Copyright 2008 The MathWorks, Inc.';\r\n\r\n        w = window.open();\r\n        d = w.document;\r\n        d.write('<pre>\\n');\r\n        d.write(code_string);\r\n\r\n        \/\/ Add author and copyright lines at the bottom if specified.\r\n        if ((author.length > 0) || (copyright.length > 0)) {\r\n            d.writeln('');\r\n            d.writeln('%%');\r\n            if (author.length > 0) {\r\n                d.writeln('% _' + author + '_');\r\n            }\r\n            if (copyright.length > 0) {\r\n                d.writeln('% _' + copyright + '_');\r\n            }\r\n        }\r\n\r\n        d.write('<\/pre>\\n');\r\n      \r\n      d.title = title + ' (MATLAB code)';\r\n      d.close();\r\n      }   \r\n      \r\n-->\r\n<\/script><p style=\"text-align: right; font-size: xx-small; font-weight:lighter;   font-style: italic; color: gray\"><br><a href=\"javascript:grabCode_404c55f1d72c40348e23c8808a3f7eba()\"><span style=\"font-size: x-small;        font-style: italic;\">Get \r\n            the MATLAB code \r\n            <noscript>(requires JavaScript)<\/noscript><\/span><\/a><br><br>\r\n      Published with MATLAB&reg; 7.6<br><\/p>\r\n<\/div>\r\n<!--\r\n404c55f1d72c40348e23c8808a3f7eba ##### SOURCE BEGIN #####\r\n%% Using MATLAB to Grade\r\n% Educators use MATLAB a lot.  In addition to using MATLAB for research,\r\n% many professors and instructors use MATLAB for teaching, including\r\n% demonstrating and explaining concepts, creating class notes, and creating\r\n% and collecting homework assignments and exams.  Today I will show how you\r\n% might use a\r\n% <https:\/\/www.mathworks.com\/help\/stats\/dataset.html |dataset| array>\r\n% from <https:\/\/www.mathworks.com\/products\/statistics\/ Statistic Toolbox> to\r\n% grade a set of student results that are already recorded.\r\n%% What is a dataset Array?\r\n% A |dataset| array is basically two-dimensional array\r\n% where each column holds data represented in a single data type, but the\r\n% columns together may comprise many different data types.  \r\n%%\r\n% A cell array\r\n% can do this, but does not enforce the idea that all the elements in a\r\n% given column must have the same type.  To have names with a cell array, you\r\n% either need to carry around an extra array, or have a special row or column\r\n% to contain the label.  \r\n%%\r\n% A scalar structure similarly can\r\n% perform this service but but does not enforce the idea that each field\r\n% must hold the same number of rows. Additionally, a |dataset| array can\r\n% have labels that allow you to reference data not just by numeric or\r\n% logical indexing, but by names as well.  \r\n%% Why Use a dataset Array?\r\n% You might want to use a |dataset| array if the data you have is natural\r\n% to think of as collection of different but related entities.  The\r\n% relationships between the collections are more constrained (in this case,\r\n% 1:1) than the flexibility afforded by cells or structs.\r\n% The example I'm showing here, grading an assignment, shows\r\n% some of the simpler ways in which you might use a |dataset| array.\r\n%% Creating a dataset\r\n% I have an assignment with 5 questions, 2 T\/F and 3 multiple choice (a:d).\r\n% The questions have extraordinarily imaginative names. \r\nqnames = {'Q1' 'Q2' 'Q3' 'Q4' 'Q5'};\r\n%%\r\n% I have collected the students' results in a sheet of a spreadsheet\r\n% labeled \"students\" and the truth in a sheet labeled \"truth\".  I can read\r\n% each of these directly into a |dataset| array so the results are\r\n% available for analysis.\r\nanswers = dataset('xlsfile','class answers.xls',...\r\n    'sheet','students','ReadObsNames',true,...\r\n    'ReadVarNames',false,'VarNames',qnames)\r\n%%\r\n% As you can see, all my students have similar names. In fact, I\r\n% believe the most common root name for employees at MathWorks is this same\r\n% root.  Each row represents the results for a given student, and the\r\n% columns represent the results for a given question.\r\n%%\r\n% Here are the \"real\" answers.\r\ntruth = dataset('xlsfile','class answers.xls',...\r\n    'sheet','truth','ReadObsNames',true,...\r\n    'ReadVarNames',false,'VarNames',qnames)\r\n\r\n%% Merge All into a Single dataset\r\n% I am placing the answer key in the top row of my array so I have the\r\n% truth handy for comparison later.\r\nalldata = [truth; answers]\r\n\r\n%% Transform the Underlying Data\r\n% As you can see, the answers show up as a mixture of string values,\r\n% 1s, and 0s.  If fact, I prefer to think of the 1s and 0s as true and\r\n% false.  Also, the string values are the results from multiple choice\r\n% questions - where the answers are limited to values *a* through *d*.\r\n% I would prefer to see the results reflected in the way I want to think\r\n% about them.  So I now transform the data, column by column.\r\nfor q = qnames\r\n    % Get all the values for a question.\r\n    vals = alldata.(q{1});\r\n    % Change numeric values to logical.\r\n    if isnumeric(vals)\r\n        alldata.(q{1}) = logical(vals);\r\n    else\r\n    % Change non-numeric values to the collection 'a':'d'\r\n        alldata.(q{1}) = nominal(vals,[],{'a','b','c','d'});\r\n    end\r\nend\r\n%%\r\n% Here's a summary of the transformed data.\r\nsummary(alldata)\r\n%%\r\n% It shows me, by column, the size and type of the data and, in this case,\r\n% a count of how many results there are for each possible value.\r\n%%\r\n% You may have noticed that I converted the multiple choice columns to a\r\n% type called\r\n% <https:\/\/www.mathworks.com\/help\/stats\/nominal.html |nominal|>.\r\n% The idea of a |nominal| array is constrain the values in the array to a\r\n% specific collection of values.  If all the acceptable values for the\r\n% array are represented in the data, you often don't need more than the\r\n% first input.  Since some of my columns did not include all possible\r\n% values a:d, I supplied these as the levels. Since they are strings, I\r\n% choose to use them as the labels for the data as well as the values.\r\n\r\n%% Gathering Information Per Question\r\n% I am now poised to gather information either by question or by student.\r\n% Let's first look at the answers for questions 1 and 4.  This is another\r\n% |dataset| array.\r\nq14Truth = alldata('Answers',{'Q1' 'Q4'})\r\n\r\n%%\r\n% To get a single answer, I have another option.  This returns a |nominal|\r\n% value.\r\nq4ans = alldata.Q4('Answers')\r\n\r\n%%\r\n% I can get all the students' answers to a particular question.  I get a\r\n% |nominal| vector in return here since the |Q4| column contains the\r\n% answers to a multiple choice question.\r\nq4all = alldata.Q4(2:end)\r\n\r\n%%\r\nwhos q4*\r\n%%\r\n% And I can also find out all answers for one student, resulting in another\r\n% |dataset| array.\r\nChrisAnswers = alldata('Chris',:)\r\n\r\n%% Which Questions are Hard?\r\n% Suppose I want to find out which questions are hardest for this set of\r\n% students.  I can use the\r\n% <https:\/\/www.mathworks.com\/access\/helpdesk\/help\/toolbox\/stats\/datasetfundataset.html |datasetfun|>\r\n% function, similar to |cellfun| and |arrayfun|, to apply a function to\r\n% each variable in the data. First I need to find out which questions\r\n% students got right and wrong so I can compare their answers to the truth\r\n% (row 1).  \r\nf = @(x) 100*sum(x(1)==x(2:end))\/(size(alldata,1)-1)\r\npercentRight = datasetfun(f,alldata)\r\n\r\n%% Score the Assignments for Each Student\r\n% use datasetfun by comparing all elements in a column for students (i.e.,\r\n% 2:end) with first element of that column, the right answer.  I make an\r\n% effort to label the rows with the students' names here.\r\nf = @(x) x(1)==x(2:end)\r\nrightWrong = datasetfun(f,alldata,'DatasetOutput',true,...\r\n    'ObsNames',alldata.Properties.ObsNames(2:end))\r\n\r\n%% \r\n% Next I sum across the rows to get scores for each student and add the\r\n% score as the last column to the |dataset|.  Remember: the first score is\r\n% for the answer key so I set that score to 100.\r\nalldata.grade = [100; ...\r\n    100*sum(double(rightWrong),2)\/size(alldata,2)]\r\n%% \r\n% Notice that |alldata| now contains a new column with numeric values\r\n% in addition to the |nominal| and |logical| ones.\r\n%% How Can You See Using a dataset Array?\r\n% Can you see applications in which you'd be able to take advantage of a\r\n% working with your data as a |dataset| array?  Let me know \r\n% <https:\/\/blogs.mathworks.com\/loren\/?p=147#respond here>.\r\n\r\n##### SOURCE END ##### 404c55f1d72c40348e23c8808a3f7eba\r\n-->","protected":false},"excerpt":{"rendered":"<p>\r\n   \r\n      Educators use MATLAB a lot.  In addition to using MATLAB for research, many professors and instructors use MATLAB for teaching,\r\n         including demonstrating and explaining concepts,... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/loren\/2008\/07\/23\/using-matlab-to-grade\/\">read more >><\/a><\/p>","protected":false},"author":39,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[25,6],"tags":[],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/147"}],"collection":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/users\/39"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/comments?post=147"}],"version-history":[{"count":3,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/147\/revisions"}],"predecessor-version":[{"id":1892,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/147\/revisions\/1892"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/media?parent=147"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/categories?post=147"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/tags?post=147"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}