{"id":129,"date":"2008-02-27T10:12:24","date_gmt":"2008-02-27T15:12:24","guid":{"rendered":"https:\/\/blogs.mathworks.com\/loren\/2008\/02\/27\/should-min-and-max-marry\/"},"modified":"2008-02-27T10:13:57","modified_gmt":"2008-02-27T15:13:57","slug":"should-min-and-max-marry","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/loren\/2008\/02\/27\/should-min-and-max-marry\/","title":{"rendered":"Should min and max Marry?"},"content":{"rendered":"<div xmlns:mwsh=\"https:\/\/www.mathworks.com\/namespace\/mcode\/v1\/syntaxhighlight.dtd\" class=\"content\">\r\n   <introduction>\r\n      <p>Do you ever need both the minimum and maximum values on the same data? Sometimes I do, for example, when I want to tweak the\r\n         limits of a 2-dimensional plot.  So I was wondering whether that is a common task? I also wondered what the overhead is in\r\n         calling <a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/min.html\"><tt>min<\/tt><\/a> and <a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/max.html\"><tt>max<\/tt><\/a> separately, instead of scanning through the data once, looking for both the lowest and highest values.\r\n      <\/p>\r\n   <\/introduction>\r\n   <h3>Contents<\/h3>\r\n   <div>\r\n      <ul>\r\n         <li><a href=\"#1\">The Plan<\/a><\/li>\r\n         <li><a href=\"#2\">Create Data<\/a><\/li>\r\n         <li><a href=\"#3\">Simplified min function<\/a><\/li>\r\n         <li><a href=\"#4\">Test mymin Function<\/a><\/li>\r\n         <li><a href=\"#5\">Test mymax Function<\/a><\/li>\r\n         <li><a href=\"#6\">Larger Data<\/a><\/li>\r\n         <li><a href=\"#7\">Combined Function<\/a><\/li>\r\n         <li><a href=\"#9\">Time Combined Function<\/a><\/li>\r\n         <li><a href=\"#10\">Compare Total Times<\/a><\/li>\r\n         <li><a href=\"#12\">Worth It?<\/a><\/li>\r\n      <\/ul>\r\n   <\/div>\r\n   <h3>The Plan<a name=\"1\"><\/a><\/h3>\r\n   <p>I will explore this idea by creating my own simplified versions of <tt>max<\/tt> and <tt>min<\/tt>, working only for real vector inputs, not checking for NaNs, and not doing any error checking.  Clearly, the code I show\r\n      is not meant for production but to get to the heart of the algorithm.\r\n   <\/p>\r\n   <h3>Create Data<a name=\"2\"><\/a><\/h3>\r\n   <p>Here I create a very small test dataset.<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">n = 10;\r\nx = rand(n,1)<\/pre><pre style=\"font-style:oblique\">x =\r\n    0.9145\r\n    0.1215\r\n    0.3765\r\n    0.7912\r\n    0.1848\r\n    0.0508\r\n    0.5562\r\n    0.4633\r\n    0.8539\r\n    0.0384\r\n<\/pre><h3>Simplified min function<a name=\"3\"><\/a><\/h3>\r\n   <p>This function is simple, as advertized.<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">type <span style=\"color: #A020F0\">mymin<\/span><\/pre><pre style=\"font-style:oblique\">\r\nfunction xmin = mymin(x)\r\n%MYMIN Minimum value of real vector input.\r\n%   MYMIN is a simplified version of MIN that\r\n%   finds the minimum value for\r\n%   real-valued vector.  NaNs are not treated.\r\nxmin = Inf;\r\nfor ind = 1:length(x)\r\n    if x(ind) &lt; xmin\r\n        xmin = x(ind);\r\n    end\r\nend\r\n\r\n<\/pre><h3>Test mymin Function<a name=\"4\"><\/a><\/h3>\r\n   <p>Now I compare the result from <tt>mymin<\/tt> to the built-in <tt>min<\/tt> function in MATLAB.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">xmin = mymin(x);\r\nxminML = min(x);\r\ntf = isequal(xmin, xminML)<\/pre><pre style=\"font-style:oblique\">tf =\r\n     1\r\n<\/pre><h3>Test mymax Function<a name=\"5\"><\/a><\/h3>\r\n   <p>Do similar testing with <tt>mymax<\/tt>.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">xmax = mymax(x);\r\nxmaxML = max(x);\r\ntf = isequal(xmax, xmaxML)<\/pre><pre style=\"font-style:oblique\">tf =\r\n     1\r\n<\/pre><h3>Larger Data<a name=\"6\"><\/a><\/h3>\r\n   <p>Now I want to do some timing so I will create a much larger array of data.<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">n = 3e7;\r\nxL = rand(n,1);\r\ntic\r\nxLmin = mymin(xL);\r\ntimeMin = toc\r\ntic\r\nxLmax = mymax(xL);\r\ntimeMax = toc<\/pre><pre style=\"font-style:oblique\">timeMin =\r\n    0.7469\r\ntimeMax =\r\n    0.7491\r\n<\/pre><h3>Combined Function<a name=\"7\"><\/a><\/h3>\r\n   <p>Now let me show you my combined function <tt>myminmax<\/tt> that loops through the data the same way <tt>mymin<\/tt> and <tt>mymax<\/tt> did, but does both calculations in the loop together.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">type <span style=\"color: #A020F0\">myminmax<\/span><\/pre><pre style=\"font-style:oblique\">\r\nfunction [xmin, xmax] = myminmax(x)\r\n%MYMINMAX Extreme values of real vector input.\r\n%   MYMINMAX is a simplified version of MIN and \r\n%   MAX combined and finds the\r\n%   minimum  and maximum values for real-valued vector.\r\n%   NaNs are not treated.\r\nxmin = Inf;\r\nxmax = -Inf;\r\nfor ind = 1:length(x)\r\n    if x(ind) &lt; xmin\r\n        xmin = x(ind);\r\n    end\r\n    if x(ind) &gt; xmax\r\n        xmax = x(ind);\r\n    end\r\nend\r\n<\/pre><p>First, let's check that we get the expected results.<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">[xminNew, xmaxNew] = myminmax(x);\r\ntf = isequal([xminNew xmaxNew], [xmin xmax])<\/pre><pre style=\"font-style:oblique\">tf =\r\n     1\r\n<\/pre><h3>Time Combined Function<a name=\"9\"><\/a><\/h3>\r\n   <p>And now let's time the combined function.<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">tic\r\n[xLminNew xLmaxNew] = myminmax(xL);\r\ntimeMinmax = toc<\/pre><pre style=\"font-style:oblique\">timeMinmax =\r\n    1.0614\r\n<\/pre><h3>Compare Total Times<a name=\"10\"><\/a><\/h3>\r\n   <p>To compare the times, let's look at the sum for the times calling the individual functions vs. calling the combined one.<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">t2 = timeMin+timeMax;\r\nformat <span style=\"color: #A020F0\">long<\/span>\r\ndisp(<span style=\"color: #A020F0\">'Adding separate times     Combined Time'<\/span>)\r\ndisp([t2 timeMinmax])<\/pre><pre style=\"font-style:oblique\">Adding separate times     Combined Time\r\n   1.495988735998570   1.061442445897453\r\n<\/pre><p>Reset format to default<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">format <span style=\"color: #A020F0\">short<\/span><\/pre><h3>Worth It?<a name=\"12\"><\/a><\/h3>\r\n   <p>Is it worth having a combined function for <tt>min<\/tt> and <tt>max<\/tt> from a speed point of view?  I don't know.  It depends on many things, including how often it's needed, and if finding the\r\n      <tt>min<\/tt> and <tt>max<\/tt> values is one of the dominant time consumers in the overall algorithm.\r\n   <\/p><script language=\"JavaScript\">\r\n<!--\r\n\r\n    function grabCode_a37895a333a3489fac180af3ca8eea0a() {\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='a37895a333a3489fac180af3ca8eea0a ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' a37895a333a3489fac180af3ca8eea0a';\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_a37895a333a3489fac180af3ca8eea0a()\"><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\na37895a333a3489fac180af3ca8eea0a ##### SOURCE BEGIN #####\r\n%% Should min and max Marry?\r\n% Do you ever need both the minimum and maximum values on the same data?\r\n% Sometimes I do, for example, when I want to tweak the limits of a\r\n% 2-dimensional plot.  So I was wondering whether that is a common task?\r\n% I also wondered what the overhead is in calling \r\n% <https:\/\/www.mathworks.com\/help\/matlab\/ref\/min.html |min|> \r\n% and <https:\/\/www.mathworks.com\/help\/matlab\/ref\/max.html |max|>\r\n% separately, instead of scanning through the data once, looking for both\r\n% the lowest and highest values.\r\n%% The Plan\r\n% I will explore this idea by creating my own simplified versions of |max|\r\n% and |min|, working only for real vector inputs, not checking for NaNs,\r\n% and not doing any error checking.  Clearly, the code I show is not meant\r\n% for production but to get to the heart of the algorithm.\r\n%% Create Data\r\n% Here I create a very small test dataset.\r\nn = 10;\r\nx = rand(n,1)\r\n%% Simplified min function\r\n% This function is simple, as advertized.\r\ntype mymin\r\n%% Test mymin Function\r\n% Now I compare the result from |mymin| to the built-in |min| function in\r\n% MATLAB.\r\nxmin = mymin(x);\r\nxminML = min(x);\r\ntf = isequal(xmin, xminML)\r\n%% Test mymax Function\r\n% Do similar testing with |mymax|.\r\nxmax = mymax(x);\r\nxmaxML = max(x);\r\ntf = isequal(xmax, xmaxML)\r\n%% Larger Data\r\n% Now I want to do some timing so I will create a much larger array of\r\n% data.\r\nn = 3e7;\r\nxL = rand(n,1);\r\ntic\r\nxLmin = mymin(xL);\r\ntimeMin = toc\r\ntic\r\nxLmax = mymax(xL);\r\ntimeMax = toc\r\n%% Combined Function\r\n% Now let me show you my combined function |myminmax| that loops through\r\n% the data the same way |mymin| and |mymax| did, but does both calculations\r\n% in the loop together.\r\ntype myminmax\r\n%%\r\n% First, let's check that we get the expected results.\r\n[xminNew, xmaxNew] = myminmax(x);\r\ntf = isequal([xminNew xmaxNew], [xmin xmax])\r\n%% Time Combined Function\r\n% And now let's time the combined function.\r\ntic\r\n[xLminNew xLmaxNew] = myminmax(xL);\r\ntimeMinmax = toc\r\n%% Compare Total Times\r\n% To compare the times, let's look at the sum for the times calling the\r\n% individual functions vs. calling the combined one.\r\nt2 = timeMin+timeMax;\r\nformat long\r\ndisp('Adding separate times     Combined Time')\r\ndisp([t2 timeMinmax])\r\n%%\r\n% Reset format to default\r\nformat short\r\n%% Worth It?\r\n% Is it worth having a combined function for |min| and |max| from a speed\r\n% point of view?  I don't know.  It depends on many things, including how\r\n% often it's needed, and if finding the |min| and |max| values is one of\r\n% the dominant time consumers in the overall algorithm.  What do you think?\r\n% Let me know <https:\/\/blogs.mathworks.com\/loren\/?p=129#respond here>.\r\n\r\n\r\n##### SOURCE END ##### a37895a333a3489fac180af3ca8eea0a\r\n-->","protected":false},"excerpt":{"rendered":"<p>\r\n   \r\n      Do you ever need both the minimum and maximum values on the same data? Sometimes I do, for example, when I want to tweak the\r\n         limits of a 2-dimensional plot.  So... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/loren\/2008\/02\/27\/should-min-and-max-marry\/\">read more >><\/a><\/p>","protected":false},"author":39,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[10],"tags":[],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/129"}],"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=129"}],"version-history":[{"count":0,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/129\/revisions"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/media?parent=129"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/categories?post=129"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/tags?post=129"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}