{"id":441,"date":"2012-05-14T03:32:02","date_gmt":"2012-05-14T08:32:02","guid":{"rendered":"https:\/\/blogs.mathworks.com\/loren\/?p=441"},"modified":"2018-01-08T15:19:29","modified_gmt":"2018-01-08T20:19:29","slug":"why-is-answer-to-3-a-7-unexpected","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/loren\/2012\/05\/14\/why-is-answer-to-3-a-7-unexpected\/","title":{"rendered":"Why is Answer to 3 < A < 7 Unexpected?"},"content":{"rendered":"<div xmlns:mwsh=\"https:\/\/www.mathworks.com\/namespace\/mcode\/v1\/syntaxhighlight.dtd\" class=\"content\">\r\n   <introduction>\r\n      <p>There have been countless (not really!) times on the <a>MATLAB newsgroup<\/a> where a question of the sort written in the title has been asked (and answered).  Let's go through the code to understand\r\n         what's happening.\r\n      <\/p>\r\n      <p><b>DISCLAIMER:<\/b> It's not my intention in this post to discuss non-scalar behavior.\r\n      <\/p>\r\n   <\/introduction>\r\n   <h3>Contents<\/h3>\r\n   <div>\r\n      <ul>\r\n         <li><a href=\"#1\">Problem Statement<\/a><\/li>\r\n         <li><a href=\"#3\">WHAT???<\/a><\/li>\r\n         <li><a href=\"#4\">First Part of Expression: low &lt; A<\/a><\/li>\r\n         <li><a href=\"#6\">Second Part of Expression: previous output &lt; high<\/a><\/li>\r\n         <li><a href=\"#8\">Look at the Types<\/a><\/li>\r\n         <li><a href=\"#10\">How to Get the Expected Answer<\/a><\/li>\r\n         <li><a href=\"#12\">Have Compound Expressions Caused You Problems?<\/a><\/li>\r\n      <\/ul>\r\n   <\/div>\r\n   <h3>Problem Statement<a name=\"1\"><\/a><\/h3>\r\n   <p>As part of program, suppose we need to see if some value lies between two others. let's say <tt>A<\/tt> is the value we are checking at the limits are <tt>low=3<\/tt> and <tt>high=7<\/tt>.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">low = 3;\r\nhigh = 7;<\/pre><p>Mathematically you might write this as<\/p><pre>  low &lt; A &lt; high<\/pre><p>Let's try that for <tt>A<\/tt> values both inside the range and outside to start. And let me place the expression into an anonymous function so I don't\r\n      have to keep repeating it.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">myExpr = @(x) low &lt; x &lt; high;\r\ninResult = myExpr(pi)\r\noutResult = myExpr(17)<\/pre><pre style=\"font-style:oblique\">inResult =\r\n     1\r\noutResult =\r\n     1\r\n<\/pre><h3>WHAT???<a name=\"3\"><\/a><\/h3>\r\n   <p>It can't be true that both <tt>pi<\/tt> and <tt>17<\/tt> lie between <tt>3<\/tt> and <tt>7<\/tt>.  So what's going on?  Let's dissect the expression.\r\n   <\/p>\r\n   <h3>First Part of Expression: low &lt; A<a name=\"4\"><\/a><\/h3>\r\n   <p>Let's look at the first part of the expression.<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">step1In = low &lt; pi\r\nstep1Out = low &lt; 17<\/pre><pre style=\"font-style:oblique\">step1In =\r\n     1\r\nstep1Out =\r\n     1\r\n<\/pre><p>and we see that this is true for both of our inputs.<\/p>\r\n   <h3>Second Part of Expression: previous output &lt; high<a name=\"6\"><\/a><\/h3>\r\n   <p>The second part of our expression uses the output from the first expression and continues from there.<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">step2In = step1In &lt; high\r\nstep2Out = step1Out &lt; high<\/pre><pre style=\"font-style:oblique\">step2In =\r\n     1\r\nstep2Out =\r\n     1\r\n<\/pre><p>and we see that we get ones, or true, for both of these.  What's going on?<\/p>\r\n   <h3>Look at the Types<a name=\"8\"><\/a><\/h3><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">whos <span style=\"color: #A020F0\">step1*<\/span><\/pre><pre style=\"font-style:oblique\">  Name          Size            Bytes  Class      Attributes\r\n\r\n  step1In       1x1                 1  logical              \r\n  step1Out      1x1                 1  logical              \r\n\r\n<\/pre><p>The results from step 1 are logical - are these numbers greater than <tt>low<\/tt>?  And the answers for both of our values is yes, or <tt>true<\/tt>, represented in MATLAB as logical values.  When we take these values as inputs in the second step, what happens is the <tt>true<\/tt> values are interpreted as numeric inputs with value 1.  And then we ask if 1 is less than <tt>high<\/tt>.  Which it is in both of these cases!\r\n   <\/p>\r\n   <h3>How to Get the Expected Answer<a name=\"10\"><\/a><\/h3>\r\n   <p>How do we get the expected answer, and it's easy.  We simply combine two logical expressions, but in a different way than\r\n      above.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">myExprCorrect = @(x) (low &lt; x) &amp; (x &lt; high)\r\ninResult1 = myExprCorrect(pi)\r\noutResult1 = myExprCorrect(17)<\/pre><pre style=\"font-style:oblique\">myExprCorrect = \r\n    @(x)(low&lt;x)&amp;(x&lt;high)\r\ninResult1 =\r\n     1\r\noutResult1 =\r\n     0\r\n<\/pre><p>What we did is checked first to see if the number was greater than <tt>low<\/tt> and separately checked the same number with <tt>high<\/tt>.  After getting two logical answers, we combine them.  They must both be true for numbers that lie between <tt>low<\/tt> and <tt>high<\/tt> and hence the result should yield <tt>true<\/tt> only under those conditions.\r\n   <\/p>\r\n   <p>For what it's worth, I always use parentheses to group my expressions to make them very readable for me so I don't need to\r\n      wonder later what I intended to be testing.\r\n   <\/p>\r\n   <h3>Have Compound Expressions Caused You Problems?<a name=\"12\"><\/a><\/h3>\r\n   <p>Let me know <a href=\"https:\/\/blogs.mathworks.com\/loren\/?p=441#respond\">here<\/a> if you've had trouble with expressions like the one in this post.\r\n   <\/p><script language=\"JavaScript\">\r\n<!--\r\n\r\n    function grabCode_a61231a8b61a49bd915aae200c8438b7() {\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='a61231a8b61a49bd915aae200c8438b7 ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' a61231a8b61a49bd915aae200c8438b7';\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 2012 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_a61231a8b61a49bd915aae200c8438b7()\"><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.14<br><\/p>\r\n<\/div>\r\n<!--\r\na61231a8b61a49bd915aae200c8438b7 ##### SOURCE BEGIN #####\r\n%% Why is Answer to 3 < A < 7 Unexpected?\r\n% There have been countless (not really!) times on the\r\n% <http:\/\/ MATLAB newsgroup>\r\n% where a question of the sort written in the title has been asked (and\r\n% answered).  Let's go through the code to understand what's happening.\r\n%\r\n% *DISCLAIMER:* It's not my intention in this post to discuss non-scalar\r\n% behavior.\r\n%% Problem Statement\r\n% As part of program, suppose we need to see if some value lies between two\r\n% others. let's say |A| is the value we are checking at the limits are\r\n% |low=3| and |high=7|.\r\nlow = 3;\r\nhigh = 7;\r\n%%\r\n% Mathematically you might write this as\r\n%\r\n%    low < A < high\r\n%\r\n% Let's try that for |A| values both inside the range and outside to start.\r\n% And let me place the expression into an anonymous function so I don't\r\n% have to keep repeating it.\r\nmyExpr = @(x) low < x < high;\r\ninResult = myExpr(pi)\r\noutResult = myExpr(17)\r\n%% WHAT???\r\n% It can't be true that both |pi| and |17| lie between |3| and |7|.  So\r\n% what's going on?  Let's dissect the expression.\r\n%\r\n%% First Part of Expression: low < A\r\n% Let's look at the first part of the expression.\r\nstep1In = low < pi\r\nstep1Out = low < 17\r\n%%\r\n% and we see that this is true for both of our inputs.\r\n%% Second Part of Expression: previous output < high\r\n% The second part of our expression uses the output from the first\r\n% expression and continues from there.\r\nstep2In = step1In < high\r\nstep2Out = step1Out < high\r\n%%\r\n% and we see that we get ones, or true, for both of these.  What's going\r\n% on?  \r\n%% Look at the Types\r\nwhos step1*\r\n%%\r\n% The results from step 1 are logical - are these numbers greater than\r\n% |low|?  And the answers for both of our values is yes, or |true|,\r\n% represented in MATLAB as logical values.  When we take these values as\r\n% inputs in the second step, what happens is the |true| values are\r\n% interpreted as numeric inputs with value 1.  And then we ask if 1 is less\r\n% than |high|.  Which it is in both of these cases!\r\n%% How to Get the Expected Answer\r\n% How do we get the expected answer, and it's easy.  We simply combine two\r\n% logical expressions, but in a different way than above.\r\nmyExprCorrect = @(x) (low < x) & (x < high)\r\ninResult1 = myExprCorrect(pi)\r\noutResult1 = myExprCorrect(17)\r\n%%\r\n% What we did is checked first to see if the number was greater than |low|\r\n% and separately checked the same number with |high|.  After getting two\r\n% logical answers, we combine them.  They must both be true for numbers\r\n% that lie between |low| and |high| and hence the result should yield\r\n% |true| only under those conditions.\r\n%\r\n% For what it's worth, I always use parentheses to group my expressions to\r\n% make them very readable for me so I don't need to wonder later what I\r\n% intended to be testing.\r\n%% Have Compound Expressions Caused You Problems?\r\n% Let me know <https:\/\/blogs.mathworks.com\/loren\/?p=441#respond here> if you've had trouble with expressions like the one\r\n% in this post.\r\n##### SOURCE END ##### a61231a8b61a49bd915aae200c8438b7\r\n-->","protected":false},"excerpt":{"rendered":"<p>\r\n   \r\n      There have been countless (not really!) times on the MATLAB newsgroup where a question of the sort written in the title has been asked (and answered).  Let's go through the code to... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/loren\/2012\/05\/14\/why-is-answer-to-3-a-7-unexpected\/\">read more >><\/a><\/p>","protected":false},"author":39,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[14],"tags":[],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/441"}],"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=441"}],"version-history":[{"count":5,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/441\/revisions"}],"predecessor-version":[{"id":2562,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/441\/revisions\/2562"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/media?parent=441"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/categories?post=441"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/tags?post=441"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}