{"id":116,"date":"2007-11-27T09:20:23","date_gmt":"2007-11-27T14:20:23","guid":{"rendered":"https:\/\/blogs.mathworks.com\/loren\/2007\/11\/27\/uniquely-matlab\/"},"modified":"2007-11-27T09:23:39","modified_gmt":"2007-11-27T14:23:39","slug":"uniquely-matlab","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/loren\/2007\/11\/27\/uniquely-matlab\/","title":{"rendered":"uniquely MATLAB"},"content":{"rendered":"<div xmlns:mwsh=\"https:\/\/www.mathworks.com\/namespace\/mcode\/v1\/syntaxhighlight.dtd\" class=\"content\">\r\n   <introduction>\r\n      <p>What makes MATLAB unique?  You have your choice regarding which element <a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/unique.html\"><tt>unique<\/tt><\/a> returns when there are repeated values in an array.  What I wonder about is why it matters.\r\n      <\/p>\r\n   <\/introduction>\r\n   <h3>Contents<\/h3>\r\n   <div>\r\n      <ul>\r\n         <li><a href=\"#1\">Which Index to Return<\/a><\/li>\r\n         <li><a href=\"#3\">unique in Action<\/a><\/li>\r\n         <li><a href=\"#6\">What I Don't Know<\/a><\/li>\r\n         <li><a href=\"#7\">Your Examples<\/a><\/li>\r\n      <\/ul>\r\n   <\/div>\r\n   <h3>Which Index to Return<a name=\"1\"><\/a><\/h3>\r\n   <p>If you look at the help for <tt>unique<\/tt><\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">helptext = help(<span style=\"color: #A020F0\">'unique'<\/span>);\r\nhelptext = helptext(1:650)<\/pre><pre style=\"font-style:oblique\">helptext =\r\n UNIQUE Set unique.\r\n    B = UNIQUE(A) for the array A returns the same values as in A but\r\n    with no repetitions. B will also be sorted. A can be a cell array of \r\n    strings.\r\n \r\n    UNIQUE(A,'rows') for the matrix A returns the unique rows of A.\r\n \r\n    [B,I,J] = UNIQUE(...) also returns index vectors I and J such\r\n    that B = A(I) and A = B(J) (or B = A(I,:) and A = B(J,:)).\r\n    \r\n    [B,I,J] = UNIQUE(...,'first') returns the vector I to index the\r\n    first occurrence of each unique value in A.  UNIQUE(...,'last'),\r\n    the default, returns the vector I to index the last occurrence.\r\n \r\n    See also UNION, INTERSECT, SETDIFF, SETXOR, ISMEMBER.\r\n\r\n \r\n<\/pre><p>you will see that you can get the index value returned.  By default, <tt>unique<\/tt> returns the last index, but you are also request the first one. Additionally, you can look for unique rows, something especially\r\n      useful for character data.\r\n   <\/p>\r\n   <h3>unique in Action<a name=\"3\"><\/a><\/h3>\r\n   <p>Here's a quick demonstration of <tt>unique<\/tt> acting on a vector of birthdates in my extended family (just the day of the month) and the unique values.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">birthdates = [11 12 22 1 14 4 25 28 1 7 19 12]\r\nbds = unique(birthdates)<\/pre><pre style=\"font-style:oblique\">birthdates =\r\n    11    12    22     1    14     4    25    28     1     7    19    12\r\nbds =\r\n     1     4     7    11    12    14    19    22    25    28\r\n<\/pre><p>Now, here are the indices back into the original <tt>birthdates<\/tt>.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">[bds, bdlast] = unique(birthdates);\r\nbdlast<\/pre><pre style=\"font-style:oblique\">bdlast =\r\n     9     6    10     1    12     5    11     3     7     8\r\n<\/pre><p>And the first occurrences.<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">[bds, bdfirst] = unique(birthdates,<span style=\"color: #A020F0\">'first'<\/span>);\r\nbdfirst<\/pre><pre style=\"font-style:oblique\">bdfirst =\r\n     4     6    10     1     2     5    11     3     7     8\r\n<\/pre><h3>What I Don't Know<a name=\"6\"><\/a><\/h3>\r\n   <p>What I can't think of is why I care about the index.  I am sure there are applications where it matters because there is related\r\n      data that needs to come along.  But what does the occurrence order have to do with anything?\r\n   <\/p>\r\n   <h3>Your Examples<a name=\"7\"><\/a><\/h3>\r\n   <p>Would you please share your code examples in which it mattered to you which instance of a non-unique element you captured?\r\n       Let me know <a href=\"https:\/\/blogs.mathworks.com\/loren\/?p=116#respond\">here<\/a>.\r\n   <\/p><script language=\"JavaScript\">\r\n<!--\r\n\r\n    function grabCode_b02f3ab7c828455280f6b6534acb904c() {\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='b02f3ab7c828455280f6b6534acb904c ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' b02f3ab7c828455280f6b6534acb904c';\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 2007 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_b02f3ab7c828455280f6b6534acb904c()\"><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.5<br><\/p>\r\n<\/div>\r\n<!--\r\nb02f3ab7c828455280f6b6534acb904c ##### SOURCE BEGIN #####\r\n%% Uniquely MATLAB\r\n% What makes MATLAB unique?  You have your choice regarding which element\r\n% <https:\/\/www.mathworks.com\/help\/matlab\/ref\/unique.html |unique|>\r\n% returns when there are repeated values in an array.  What I wonder about\r\n% is why it matters.\r\n%% Which Index to Return\r\n% If you look at the help for |unique|\r\nhelptext = help('unique');\r\nhelptext = helptext(1:650)\r\n%%\r\n% you will see that you can get the index value returned.  By default,\r\n% |unique| returns the last index, but you are also request the first one.\r\n% Additionally, you can look for unique rows, something especially useful\r\n% for character data.\r\n%% unique in Action\r\n% Here's a quick demonstration of |unique| acting on a vector of birthdates\r\n% in my extended family (just the day of the month) and the unique values.\r\nbirthdates = [11 12 22 1 14 4 25 28 1 7 19 12]\r\nbds = unique(birthdates)\r\n%%\r\n% Now, here are the indices back into the original |birthdates|.\r\n[bds, bdlast] = unique(birthdates);\r\nbdlast\r\n%%\r\n% And the first occurrences.\r\n[bds, bdfirst] = unique(birthdates,'first');\r\nbdfirst\r\n%% What I Don't Know\r\n% What I can't think of is why I care about the index.  I am sure there are\r\n% applications where it matters because there is related data that needs to\r\n% come along.  But what does the occurrence order have to do with anything?\r\n%% Your Examples\r\n% Would you please share your code examples in which it mattered to you\r\n% which instance of a non-unique element you captured?  Let me know\r\n% <https:\/\/blogs.mathworks.com\/loren\/?p=116#respond here>.\r\n##### SOURCE END ##### b02f3ab7c828455280f6b6534acb904c\r\n-->","protected":false},"excerpt":{"rendered":"<p>\r\n   \r\n      What makes MATLAB unique?  You have your choice regarding which element unique returns when there are repeated values in an array.  What I wonder about is why it matters.\r\n      \r\n   \r\n... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/loren\/2007\/11\/27\/uniquely-matlab\/\">read more >><\/a><\/p>","protected":false},"author":39,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[15],"tags":[],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/116"}],"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=116"}],"version-history":[{"count":0,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/116\/revisions"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/media?parent=116"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/categories?post=116"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/tags?post=116"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}