{"id":1014,"date":"2014-10-09T07:36:14","date_gmt":"2014-10-09T12:36:14","guid":{"rendered":"https:\/\/blogs.mathworks.com\/loren\/?p=1014"},"modified":"2014-09-29T07:36:58","modified_gmt":"2014-09-29T12:36:58","slug":"reversal-of-sorts-new-in-release-r2014b","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/loren\/2014\/10\/09\/reversal-of-sorts-new-in-release-r2014b\/","title":{"rendered":"Reversal of Sorts &#8211; New in Release R2014b"},"content":{"rendered":"\r\n<div class=\"content\"><!--introduction--><p>I wanted to show you a glimpse of some of the new math functionality available in R2014b.<\/p><!--\/introduction--><h3>Contents<\/h3><div><ul><li><a href=\"#b8d8352c-9504-4750-83f5-8f040a3f8e95\">The Question<\/a><\/li><li><a href=\"#0ffaaa36-04f1-4b95-bb45-1b230fdbd778\">My Original Answer<\/a><\/li><li><a href=\"#d758369e-9b17-48fc-a891-95bf02b3faf1\">Solution with R2014b<\/a><\/li><li><a href=\"#1f8d818d-a1f9-4c6e-ab75-78cf163601df\">What New Math Have You Enjoyed in R2014b?<\/a><\/li><\/ul><\/div><h4>The Question<a name=\"b8d8352c-9504-4750-83f5-8f040a3f8e95\"><\/a><\/h4><p>Recently on the MATLAB newsgroup, Christoph asked this question:<\/p><p><i>I have a vector A shown below, which has 6 elements. the elements  are already sorted in descending order. now i want to create vector  C by deleting elements from A, starting with element a1, until the  sum of the vector equals or is smaller the value B<\/i><\/p><pre class=\"language-matlab\">A=\r\n26\r\n23\r\n20\r\n19\r\n15\r\n14\r\n<\/pre><pre>B=70<\/pre><p><i>So, the output should be<\/i><\/p><pre>C=\r\n20\r\n19\r\n15\r\n14<\/pre><p><i>Any idea how to do this?<\/i><\/p><h4>My Original Answer<a name=\"0ffaaa36-04f1-4b95-bb45-1b230fdbd778\"><\/a><\/h4><p><i>I would do something like this.  Get the numbers in increasing order  and perform a cumulative sum.<\/i><\/p><pre>Aflip = flipud(A);\r\nAsums = cumsum(Aflip);<\/pre><p><i>Find the first sum that is GREATER than b.  The next element is where  you want to start.<\/i>  startIndex = find(Asums &gt; b, 1, 'first');<\/p><p><i>But things are reversed now.  So we need to find the starting index into the original array.<\/i>  Aind = length(A) - startIndex + 1;<\/p><p><i>Now delete the leading values you don&#8217;t want.<\/i><\/p><pre>Afiltered = A((Aind+1):end);<\/pre><h4>Solution with R2014b<a name=\"d758369e-9b17-48fc-a891-95bf02b3faf1\"><\/a><\/h4><p>In R2014b, the function <a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/cumsum.html\"><tt>cumsum<\/tt><\/a> has a new option, <tt>direction<\/tt>. (So do some other functions in the \"|cum|\" category.)  You can specify to do the calculation in the default or <tt>'forward'<\/tt> direction, or in <tt>'reverse'<\/tt>.  Here's what the new solution looks like.<\/p><pre class=\"codeinput\">A = [\r\n  26\r\n  23\r\n  20\r\n  19\r\n  15\r\n  14];\r\nB = 70;\r\ntmpA = cumsum(A,<span class=\"string\">'reverse'<\/span>)\r\nind = find(tmpA &gt; B, 1,<span class=\"string\">'last'<\/span>)\r\nC = A;\r\nC(1:ind) = []\r\n<\/pre><pre class=\"codeoutput\">tmpA =\r\n   117\r\n    91\r\n    68\r\n    48\r\n    29\r\n    14\r\nind =\r\n     2\r\nC =\r\n    20\r\n    19\r\n    15\r\n    14\r\n<\/pre><h4>What New Math Have You Enjoyed in R2014b?<a name=\"1f8d818d-a1f9-4c6e-ab75-78cf163601df\"><\/a><\/h4><p>Have you had a chance to explore some of the math functionality in R2014b?  Which features did you find helpful?  Let me know <a href=\"https:\/\/blogs.mathworks.com\/loren\/?p=1014#respond\">here<\/a>.<\/p><script language=\"JavaScript\"> <!-- \r\n    function grabCode_dbc8971ae1554550a3c87e25b76c7f75() {\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='dbc8971ae1554550a3c87e25b76c7f75 ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' dbc8971ae1554550a3c87e25b76c7f75';\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 2014 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_dbc8971ae1554550a3c87e25b76c7f75()\"><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; R2014b<br><\/p><\/div><!--\r\ndbc8971ae1554550a3c87e25b76c7f75 ##### SOURCE BEGIN #####\r\n%% Reversal of Sorts - New in Release R2014b\r\n% I wanted to show you a glimpse of some of the new math functionality\r\n% available in R2014b.  \r\n%% The Question\r\n% Recently on the MATLAB newsgroup, Christoph asked\r\n% this question:\r\n%\r\n% _I have a vector A shown below, which has 6 elements. the elements \r\n%  are already sorted in descending order. now i want to create vector \r\n%  C by deleting elements from A, starting with element a1, until the \r\n%  sum of the vector equals or is smaller the value B_\r\n%\r\n%   A=\r\n%   26\r\n%   23\r\n%   20\r\n%   19\r\n%   15\r\n%   14\r\n%\r\n%  B=70\r\n%\r\n% _So, the output should be_\r\n%\r\n%  C=\r\n%  20\r\n%  19\r\n%  15\r\n%  14\r\n% \r\n% _Any idea how to do this?_\r\n%% My Original Answer\r\n%\r\n% _I would do something like this.  Get the numbers in increasing order \r\n%  and perform a cumulative sum._\r\n%   \r\n%  Aflip = flipud(A);\r\n%  Asums = cumsum(Aflip);\r\n%  \r\n% _Find the first sum that is GREATER than b.  The next element is where \r\n%  you want to start._\r\n%  startIndex = find(Asums > b, 1, 'first');\r\n%  \r\n% _But things are reversed now.  So we need to find the starting index \r\n% into the original array._\r\n%  Aind = length(A) - startIndex + 1;\r\n%  \r\n% _Now delete the leading values you don\u00e2\u20ac\u2122t want._\r\n%\r\n%  Afiltered = A((Aind+1):end);\r\n%  \r\n%% Solution with R2014b\r\n% In R2014b, the function\r\n% <https:\/\/www.mathworks.com\/help\/matlab\/ref\/cumsum.html |cumsum|> has a\r\n% new option, |direction|. (So do some other functions in the \"|cum|\"\r\n% category.)  You can specify to do the calculation in the default or\r\n% |'forward'| direction, or in |'reverse'|.  Here's what the new solution\r\n% looks like.\r\nA = [\r\n  26\r\n  23\r\n  20\r\n  19\r\n  15\r\n  14];\r\nB = 70;\r\ntmpA = cumsum(A,'reverse')\r\nind = find(tmpA > B, 1,'last')\r\nC = A;\r\nC(1:ind) = []\r\n%% What New Math Have You Enjoyed in R2014b?\r\n% Have you had a chance to explore some of the math functionality in\r\n% R2014b?  Which features did you find helpful?  Let me know\r\n% <https:\/\/blogs.mathworks.com\/loren\/?p=1014#respond here>.\r\n\r\n\r\n\r\n\r\n##### SOURCE END ##### dbc8971ae1554550a3c87e25b76c7f75\r\n-->","protected":false},"excerpt":{"rendered":"<!--introduction--><p>I wanted to show you a glimpse of some of the new math functionality available in R2014b.... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/loren\/2014\/10\/09\/reversal-of-sorts-new-in-release-r2014b\/\">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\/1014"}],"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=1014"}],"version-history":[{"count":8,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/1014\/revisions"}],"predecessor-version":[{"id":1022,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/1014\/revisions\/1022"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/media?parent=1014"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/categories?post=1014"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/tags?post=1014"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}