{"id":2362,"date":"2008-12-19T16:20:38","date_gmt":"2008-12-19T16:20:38","guid":{"rendered":"https:\/\/blogs.mathworks.com\/pick\/2008\/12\/19\/counting-elements\/"},"modified":"2008-12-19T16:20:38","modified_gmt":"2008-12-19T16:20:38","slug":"counting-elements","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/pick\/2008\/12\/19\/counting-elements\/","title":{"rendered":"Counting Elements"},"content":{"rendered":"<div xmlns:mwsh=\"https:\/\/www.mathworks.com\/namespace\/mcode\/v1\/syntaxhighlight.dtd\" class=\"content\">\r\n   <introduction>\r\n      <p><a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/authors\/15007\">Jiro<\/a>'s pick this week is <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/7738-countmember-a-b-\">COUNTMEMBER<\/a> by <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/authors\/10584\">Jos<\/a>.\r\n      <\/p>\r\n   <\/introduction>\r\n   <p>A while back, I wrote a Pick of the Week <a href=\"https:\/\/blogs.mathworks.com\/pick\/2008\/07\/10\/a-template-saves-you-time\/\">post<\/a> on a file that created a MATLAB file template. In that post, I talked a bit about a \"best practice\" for formatting your MATLAB\r\n      files: things like, having an H1 line, including help text, including examples, etc.\r\n   <\/p>\r\n   <p>How is that related to this week's pick? Well, I chose <tt>countmember<\/tt> because Jos writes very well-written files. (I could have chosen some of his other files, but I liked the code also.) Open\r\n      the file up and see it for yourself. He has a simple, but descriptive H1 line so that <tt>LOOKFOR<\/tt> will be able to find it. The help block follows the format that many MathWorks MATLAB files use: capitalized syntax (but\r\n      case-sensitive example code), the examples can be directly executed (highlight and F9), and he has a \"See also\" section for\r\n      related functions.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">help <span style=\"color: #A020F0\">countmember<\/span><\/pre><pre style=\"font-style:oblique\">  COUNTMEMBER - count members\r\n \r\n    C = COUNTMEMBER(A,B) counts the number of times the elements of A occurs\r\n    in B, such the Ci = sum(B==Ai). C has the same size and shape as A.\r\n    A may contain non-unique elements. A and B should be of the same type. \r\n    A can be be a cell array.\r\n \r\n    Examples:\r\n      countmember([1 2 1 3],[1 2 2 2 2]) \r\n         -&gt; 1     4     1     0\r\n      countmember({'a','b','c'},{'a','x','a'}) \r\n         -&gt; 2     0     0\r\n \r\n    See also ISMEMBER, UNIQUE, HISTC\r\n \r\n    2005 Jos van der Geest\r\n\r\n<\/pre><p>Of course, the other reason I selected this was his elegant solution to the task of counting the occurrence of elements. The\r\n      code has a nice error checking mechanism and deals with empty input arguments. The code is vectorized (no for-loops) and uses\r\n      <tt>UNIQUE<\/tt> to eliminate redundant search. Also, notice the little trick to reduce overhead in his lines of code for <tt>UNIQUE<\/tt> and <tt>ISMEMBER<\/tt>.\r\n   <\/p>\r\n   <p><a href=\"https:\/\/blogs.mathworks.com\/pick\/?p=2362#respond\">Comments<\/a>?\r\n   <\/p>\r\n   <p><i><b>We will be taking next Friday off for holiday, but we'll be back next year on the 2nd of January. Have a safe and happy holiday!<\/b><\/i><\/p><script language=\"JavaScript\">\r\n<!--\r\n\r\n    function grabCode_17c0ea91ca1940df8c478872a4769ae9() {\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='17c0ea91ca1940df8c478872a4769ae9 ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' 17c0ea91ca1940df8c478872a4769ae9';\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 = 'Jiro Doke';\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_17c0ea91ca1940df8c478872a4769ae9()\"><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.7<br><\/p>\r\n<\/div>\r\n<!--\r\n17c0ea91ca1940df8c478872a4769ae9 ##### SOURCE BEGIN #####\r\n%%\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/authors\/15007\r\n% Jiro>'s pick this week is\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/7738-countmember-a-b-\r\n% COUNTMEMBER> by\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/authors\/10584\r\n% Jos>.\r\n\r\n%%\r\n% A while back, I wrote a Pick of the Week <https:\/\/blogs.mathworks.com\/pick\/2008\/07\/10\/a-template-saves-you-time\/ \r\n% post> on a file that created a MATLAB file template. In that post, I\r\n% talked a bit about a \"best practice\" for formatting your MATLAB files:\r\n% things like, having an H1 line, including help text, including examples,\r\n% etc.\r\n%\r\n% How is that related to this week's pick? Well, I chose |countmember|\r\n% because Jos writes very well-written files. (I could have chosen some of\r\n% his other files, but I liked the code also.) Open the file up and see it\r\n% for yourself. He has a simple, but descriptive H1 line so that |LOOKFOR|\r\n% will be able to find it. The help block follows the format that many\r\n% MathWorks MATLAB files use: capitalized syntax (but case-sensitive\r\n% example code), the examples can be directly executed (highlight and F9),\r\n% and he has a \"See also\" section for related functions.\r\n\r\nhelp countmember\r\n\r\n%%\r\n% Of course, the other reason I selected this was his elegant solution to\r\n% the task of counting the occurrence of elements. The code has a nice\r\n% error checking mechanism and deals with empty input arguments. The code\r\n% is vectorized (no for-loops) and uses |UNIQUE| to eliminate redundant\r\n% search. Also, notice the little trick to reduce overhead in his lines of\r\n% code for |UNIQUE| and |ISMEMBER|.\r\n%\r\n% <https:\/\/blogs.mathworks.com\/pick\/?p=2362#respond Comments>?\r\n%\r\n% _*We will be taking next Friday off for holiday, but we'll be back next\r\n% year on the 2nd of January. Have a safe and happy holiday!*_\r\n\r\n##### SOURCE END ##### 17c0ea91ca1940df8c478872a4769ae9\r\n-->","protected":false},"excerpt":{"rendered":"<p>\r\n   \r\n      Jiro's pick this week is COUNTMEMBER by Jos.\r\n      \r\n   \r\n   A while back, I wrote a Pick of the Week post on a file that created a MATLAB file template. In... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/pick\/2008\/12\/19\/counting-elements\/\">read more >><\/a><\/p>","protected":false},"author":35,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[16],"tags":[],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/2362"}],"collection":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/users\/35"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/comments?post=2362"}],"version-history":[{"count":0,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/2362\/revisions"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/media?parent=2362"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/categories?post=2362"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/tags?post=2362"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}