{"id":1359,"date":"2016-03-03T13:38:41","date_gmt":"2016-03-03T18:38:41","guid":{"rendered":"https:\/\/blogs.mathworks.com\/loren\/?p=1359"},"modified":"2016-03-23T14:34:23","modified_gmt":"2016-03-23T19:34:23","slug":"uniqueness-and-membership","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/loren\/2016\/03\/03\/uniqueness-and-membership\/","title":{"rendered":"Uniqueness and Membership"},"content":{"rendered":"<div class=\"content\"><!--introduction--><p>We have had many requests over the years for some of our functions to include tolerances, e.g., <a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/find.html\"><tt>find<\/tt><\/a>.  I don't know if you noticed, but some of the relevant functions (not <tt>find<\/tt>) now allow this.<\/p><!--\/introduction--><h3>Contents<\/h3><div><ul><li><a href=\"#4edf7c01-1a47-4cf0-9398-f8e713a4c60f\">uniquetol and ismembertol<\/a><\/li><li><a href=\"#edb3c022-bf37-46e7-b9c2-8c5dd4a33044\">Specify Data Scaling by Column<\/a><\/li><li><a href=\"#a5a62a76-588d-4bc0-83ba-530c1a8b2c9f\">Plot the Results<\/a><\/li><li><a href=\"#46eb048c-593b-4b17-a719-87230197e667\">Your Turn<\/a><\/li><\/ul><\/div><h4>uniquetol and ismembertol<a name=\"4edf7c01-1a47-4cf0-9398-f8e713a4c60f\"><\/a><\/h4><p>Recently (R2015a), we added <a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/ismembertol.html\"><tt>ismembertol<\/tt><\/a> and <a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/uniquetol.html\"><tt>uniquetol<\/tt><\/a> to MATLAB.  This is to reflect the notion that, especially with floating point arithmetic, we should not generally expect quantities that we compare to be exact matches, but close, to within an tolerance.  You can argue that |find|ing something might sound a bit more specific, hence no tolerance there.  Not sure if that holds up. And, of course, there's always <a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/eps.html\"><tt>eps<\/tt><\/a>. So now there are some more good tools for you to use!<\/p><h4>Specify Data Scaling by Column<a name=\"edb3c022-bf37-46e7-b9c2-8c5dd4a33044\"><\/a><\/h4><p>Let's suppose we have some data that we want to group into different vertical bands.  The rows of <tt>A<\/tt> and <tt>B<\/tt> contain the x and y values of the points.  Because of this, we set <tt>'ByRows'<\/tt> to be <tt>true<\/tt>.  And we want all points included; hence <tt>'OutputAllIndices'<\/tt> is also <tt>true<\/tt>. Finally we use the <tt>'DataScale'<\/tt> to be unbounded at the high end for <tt>y<\/tt> so we can ignore those values, but <tt>1<\/tt> for an absolute tolerance for <tt>x<\/tt>.<\/p><pre class=\"codeinput\">A = rand(1000,2);\r\nB = [(0:.2:1)',0.5*ones(6,1)];\r\n[LIA,LocAllB] = ismembertol(B, A, 0.1, <span class=\"string\">'ByRows'<\/span>, true, <span class=\"keyword\">...<\/span>\r\n    <span class=\"string\">'OutputAllIndices'<\/span>, true, <span class=\"string\">'DataScale'<\/span>, [1,Inf])\r\n<\/pre><pre class=\"codeoutput\">LIA =\r\n     1\r\n     1\r\n     1\r\n     1\r\n     1\r\n     1\r\nLocAllB = \r\n    [103x1 double]\r\n    [185x1 double]\r\n    [188x1 double]\r\n    [213x1 double]\r\n    [212x1 double]\r\n    [ 99x1 double]\r\n<\/pre><p>You can see we get several bands of data, with different amounts in each bin of <tt>LocallB<\/tt>.<\/p><h4>Plot the Results<a name=\"a5a62a76-588d-4bc0-83ba-530c1a8b2c9f\"><\/a><\/h4><p>Now we want to plot the points in <tt>A<\/tt> that are within the distance of the stated tolerance for each point in <tt>B<\/tt>.<\/p><pre class=\"codeinput\">hold <span class=\"string\">on<\/span>\r\nplot(B(:,1),B(:,2),<span class=\"string\">'x'<\/span>)\r\n<span class=\"keyword\">for<\/span> k = 1:length(LocAllB)\r\n    plot(A(LocAllB{k},1), A(LocAllB{k},2),<span class=\"string\">'.'<\/span>)\r\n<span class=\"keyword\">end<\/span>\r\nhold <span class=\"string\">off<\/span>\r\n<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/loren\/2016\/beTolerant_01.png\" alt=\"\"> <h4>Your Turn<a name=\"46eb048c-593b-4b17-a719-87230197e667\"><\/a><\/h4><p>Have you found good uses for these new functions that let you look for relevant candidates within some tolerance?  Let me know <a href=\"https:\/\/blogs.mathworks.com\/loren\/?p=1359#respond\">here<\/a>.<\/p><script language=\"JavaScript\"> <!-- \r\n    function grabCode_285f219d8255498c9abbeb50a59aabc8() {\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='285f219d8255498c9abbeb50a59aabc8 ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' 285f219d8255498c9abbeb50a59aabc8';\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        copyright = 'Copyright 2016 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 copyright line at the bottom if specified.\r\n        if (copyright.length > 0) {\r\n            d.writeln('');\r\n            d.writeln('%%');\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     --> <\/script><p style=\"text-align: right; font-size: xx-small; font-weight:lighter;   font-style: italic; color: gray\"><br><a href=\"javascript:grabCode_285f219d8255498c9abbeb50a59aabc8()\"><span style=\"font-size: x-small;        font-style: italic;\">Get \r\n      the MATLAB code <noscript>(requires JavaScript)<\/noscript><\/span><\/a><br><br>\r\n      Published with MATLAB&reg; R2015b<br><\/p><\/div><!--\r\n285f219d8255498c9abbeb50a59aabc8 ##### SOURCE BEGIN #####\r\n%% Uniqueness and Membership\r\n% We have had many requests over the years for some of our functions to\r\n% include tolerances, e.g.,\r\n% <https:\/\/www.mathworks.com\/help\/matlab\/ref\/find.html |find|>.  I don't\r\n% know if you noticed, but some of the relevant functions (not |find|) now\r\n% allow this.\r\n%% uniquetol and ismembertol\r\n% Recently (R2015a), we added\r\n% <https:\/\/www.mathworks.com\/help\/matlab\/ref\/ismembertol.html |ismembertol|>\r\n% and <https:\/\/www.mathworks.com\/help\/matlab\/ref\/uniquetol.html |uniquetol|>\r\n% to MATLAB.  This is to reflect the notion that, especially with floating\r\n% point arithmetic, we should not generally expect quantities that we\r\n% compare to be exact matches, but close, to within an tolerance.  You can\r\n% argue that |find|ing something might sound a bit more specific, hence no\r\n% tolerance there.  Not sure if that holds up. And, of course, there's\r\n% always <https:\/\/www.mathworks.com\/help\/matlab\/ref\/eps.html |eps|>. So\r\n% now there are some more good tools for you to use!\r\n%% Specify Data Scaling by Column\r\n% Let's suppose we have some data that we want to group into different\r\n% vertical bands.  The rows of |A| and |B| contain the x and y values of\r\n% the points.  Because of this, we set |'ByRows'| to be |true|.  And we\r\n% want all points included; hence |'OutputAllIndices'| is also |true|.\r\n% Finally we use the |'DataScale'| to be unbounded at the high end for |y|\r\n% so we can ignore those values, but |1| for an absolute tolerance for |x|.\r\nA = rand(1000,2);\r\nB = [(0:.2:1)',0.5*ones(6,1)];\r\n[LIA,LocAllB] = ismembertol(B, A, 0.1, 'ByRows', true, ...\r\n    'OutputAllIndices', true, 'DataScale', [1,Inf])\r\n\r\n%%\r\n% You can see we get several bands of data, with different amounts in each\r\n% bin of |LocallB|.\r\n\r\n\r\n%% Plot the Results\r\n% Now we want to plot the points in |A| that are within the distance of the\r\n% stated tolerance for each point in |B|.\r\nhold on\r\nplot(B(:,1),B(:,2),'x')\r\nfor k = 1:length(LocAllB)\r\n    plot(A(LocAllB{k},1), A(LocAllB{k},2),'.')\r\nend\r\nhold off\r\n%% Your Turn\r\n% Have you found good uses for these new functions that let you look for\r\n% relevant candidates within some tolerance?  Let me know\r\n% <https:\/\/blogs.mathworks.com\/loren\/?p=1359#respond here>.\r\n\r\n\r\n##### SOURCE END ##### 285f219d8255498c9abbeb50a59aabc8\r\n-->","protected":false},"excerpt":{"rendered":"<div class=\"overview-image\"><img decoding=\"async\"  class=\"img-responsive\" src=\"https:\/\/blogs.mathworks.com\/images\/loren\/2016\/beTolerant_01.png\" onError=\"this.style.display ='none';\" \/><\/div><!--introduction--><p>We have had many requests over the years for some of our functions to include tolerances, e.g., <a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/find.html\"><tt>find<\/tt><\/a>.  I don't know if you noticed, but some of the relevant functions (not <tt>find<\/tt>) now allow this.... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/loren\/2016\/03\/03\/uniqueness-and-membership\/\">read more >><\/a><\/p>","protected":false},"author":39,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[6],"tags":[],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/1359"}],"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=1359"}],"version-history":[{"count":1,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/1359\/revisions"}],"predecessor-version":[{"id":1360,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/1359\/revisions\/1360"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/media?parent=1359"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/categories?post=1359"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/tags?post=1359"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}