{"id":203,"date":"2009-10-21T17:53:32","date_gmt":"2009-10-21T17:53:32","guid":{"rendered":"https:\/\/blogs.mathworks.com\/loren\/2009\/10\/21\/dealing-with-cells\/"},"modified":"2016-08-04T08:26:12","modified_gmt":"2016-08-04T13:26:12","slug":"dealing-with-cells","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/loren\/2009\/10\/21\/dealing-with-cells\/","title":{"rendered":"Dealing with Cells"},"content":{"rendered":"<div xmlns:mwsh=\"https:\/\/www.mathworks.com\/namespace\/mcode\/v1\/syntaxhighlight.dtd\" class=\"content\">\r\n   <introduction>\r\n      <p>A customer recently asked me this question at the MATLAB Virtual Conference.\r\n      <\/p>\r\n   <\/introduction>\r\n   <h3>Contents<\/h3>\r\n   <div>\r\n      <ul>\r\n         <li><a href=\"#1\">Question about Summing Cell Rows<\/a><\/li>\r\n         <li><a href=\"#2\">Example<\/a><\/li>\r\n         <li><a href=\"#3\">Answer<\/a><\/li>\r\n         <li><a href=\"#8\">Cell Array Questions<\/a><\/li>\r\n      <\/ul>\r\n   <\/div>\r\n   <h3>Question about Summing Cell Rows<a name=\"1\"><\/a><\/h3><pre>  I was hoping you would cover cells some day. Here is a particular\r\n  problem I was hoping to have a more elegant solutions for.<\/pre><pre>  A is a cell that has String (say names) in the first column, Numbers\r\n  (say scores in tests 1 and 2) in the next two columns. Assume that each\r\n  cell has only one value. Is there an easier way to calculate, say the\r\n  sum of the two test scores than a for loop?<\/pre><h3>Example<a name=\"2\"><\/a><\/h3>\r\n   <p>Let's make some sample data.<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">c(1:4,1) = {<span style=\"color: #A020F0\">'Fred'<\/span> <span style=\"color: #A020F0\">'Alice'<\/span> <span style=\"color: #A020F0\">'Lucy'<\/span> <span style=\"color: #A020F0\">'Tom'<\/span>};\r\nc(1:4,2) = { 90  80  55 102 };\r\nc(1:4,3) = { 43  91  80  44 }<\/pre><pre style=\"font-style:oblique\">c = \r\n    'Fred'     [ 90]    [43]\r\n    'Alice'    [ 80]    [91]\r\n    'Lucy'     [ 55]    [80]\r\n    'Tom'      [102]    [44]\r\n<\/pre><h3>Answer<a name=\"3\"><\/a><\/h3>\r\n   <p>To sum the entries in a row for this <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2009b\/techdoc\/matlab_prog\/br04bw6-98.html\"><tt>cell array<\/tt><\/a>, I can simply turn the numeric values into a numeric array via comma-separated list notation, and then sum the rows.  Let's\r\n      see the pieces for clarity.  Here's the comma-separated list.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">c{:,2:3}<\/pre><pre style=\"font-style:oblique\">ans =\r\n    90\r\nans =\r\n    80\r\nans =\r\n    55\r\nans =\r\n   102\r\nans =\r\n    43\r\nans =\r\n    91\r\nans =\r\n    80\r\nans =\r\n    44\r\n<\/pre><p>Now place the results into a vector.<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">vec = [c{:,2:3}]<\/pre><pre style=\"font-style:oblique\">vec =\r\n    90    80    55   102    43    91    80    44\r\n<\/pre><p>Reshape the vector appropriately into a matrix.<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">array = reshape(vec,[],2)<\/pre><pre style=\"font-style:oblique\">array =\r\n    90    43\r\n    80    91\r\n    55    80\r\n   102    44\r\n<\/pre><p>Now sum the results along the rows.<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">tot = sum(array,2)<\/pre><pre style=\"font-style:oblique\">tot =\r\n   133\r\n   171\r\n   135\r\n   146\r\n<\/pre><p>Or, in one bigger statement, try this.<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">tot = sum(reshape([c{:,2:3}],[],2),2)<\/pre><pre style=\"font-style:oblique\">tot =\r\n   133\r\n   171\r\n   135\r\n   146\r\n<\/pre><h3>Cell Array Questions<a name=\"8\"><\/a><\/h3>\r\n   <p>Do you use cell arrays?  Can you do what you want without undo contortions?  Let me know <a href=\"https:\/\/blogs.mathworks.com\/loren\/?p=203#respond\">here<\/a>.\r\n   <\/p><script language=\"JavaScript\">\r\n<!--\r\n\r\n    function grabCode_6f695f938464405ea30ed8125cd1b1ef() {\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='6f695f938464405ea30ed8125cd1b1ef ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' 6f695f938464405ea30ed8125cd1b1ef';\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 2009 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_6f695f938464405ea30ed8125cd1b1ef()\"><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.9<br><\/p>\r\n<\/div>\r\n<!--\r\n6f695f938464405ea30ed8125cd1b1ef ##### SOURCE BEGIN #####\r\n%% Dealing with Cells\r\n% A customer recently asked me this question at the \r\n% <http:\/\/events.unisfair.com\/rt\/matlab~virtualconf?code=hp-listing MATLAB Virtual Conference>.\r\n%\r\n%% Question about Summing Cell Rows\r\n%    I was hoping you would cover cells some day. Here is a particular\r\n%    problem I was hoping to have a more elegant solutions for. \r\n%\r\n%    A is a cell that has String (say names) in the first column, Numbers\r\n%    (say scores in tests 1 and 2) in the next two columns. Assume that each\r\n%    cell has only one value. Is there an easier way to calculate, say the\r\n%    sum of the two test scores than a for loop?\r\n%% Example\r\n% Let's make some sample data.\r\nc(1:4,1) = {'Fred' 'Alice' 'Lucy' 'Tom'};\r\nc(1:4,2) = { 90  80  55 102 };\r\nc(1:4,3) = { 43  91  80  44 }\r\n%% Answer\r\n% To sum the entries in a row for this \r\n% <https:\/\/www.mathworks.com\/help\/releases\/R2009b\/techdoc\/matlab_prog\/br04bw6-98.html |cell array|>, \r\n% I can simply turn the numeric values into a numeric array\r\n% via comma-separated list notation, and then sum the rows.  Let's see\r\n% the pieces for clarity.  Here's the comma-separated list.\r\nc{:,2:3}\r\n%%\r\n% Now place the results into a vector.\r\nvec = [c{:,2:3}]\r\n%%\r\n% Reshape the vector appropriately into a matrix.\r\narray = reshape(vec,[],2)\r\n%%\r\n% Now sum the results along the rows.\r\ntot = sum(array,2)\r\n%%\r\n% Or, in one bigger statement, try this.\r\ntot = sum(reshape([c{:,2:3}],[],2),2)\r\n\r\n%% Cell Array Questions\r\n% Do you use cell arrays?  Can you do what you want without undo\r\n% contortions?  Let me know\r\n% <https:\/\/blogs.mathworks.com\/loren\/?p=203#respond here>.\r\n\r\n##### SOURCE END ##### 6f695f938464405ea30ed8125cd1b1ef\r\n-->","protected":false},"excerpt":{"rendered":"<p>\r\n   \r\n      A customer recently asked me this question at the MATLAB Virtual Conference.\r\n      \r\n   \r\n   Contents\r\n   \r\n      \r\n       ... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/loren\/2009\/10\/21\/dealing-with-cells\/\">read more >><\/a><\/p>","protected":false},"author":39,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[17],"tags":[],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/203"}],"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=203"}],"version-history":[{"count":1,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/203\/revisions"}],"predecessor-version":[{"id":1939,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/203\/revisions\/1939"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/media?parent=203"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/categories?post=203"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/tags?post=203"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}