{"id":577,"date":"2012-11-14T14:16:17","date_gmt":"2012-11-14T19:16:17","guid":{"rendered":"https:\/\/blogs.mathworks.com\/loren\/?p=577"},"modified":"2018-01-08T15:28:35","modified_gmt":"2018-01-08T20:28:35","slug":"partitioning-a-vector","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/loren\/2012\/11\/14\/partitioning-a-vector\/","title":{"rendered":"Partitioning a Vector"},"content":{"rendered":"<!DOCTYPE html\r\n  PUBLIC \"-\/\/W3C\/\/DTD HTML 4.01 Transitional\/\/EN\">\r\n<style type=\"text\/css\">\r\n\r\nh1 { font-size:18pt; }\r\nh2.titlebg { font-size:13pt; }\r\nh3 { color:#4A4F55; padding:0px; margin:5px 0px 5px; font-family:Arial, Helvetica, sans-serif; font-size:11pt; font-weight:bold; line-height:140%; border-bottom:1px solid #d6d4d4; display:block; }\r\nh4 { color:#4A4F55; padding:0px; margin:0px 0px 5px; font-family:Arial, Helvetica, sans-serif; font-size:10pt; font-weight:bold; line-height:140%; border-bottom:1px solid #d6d4d4; display:block; }\r\n   \r\np { padding:0px; margin:0px 0px 20px; }\r\nimg { padding:0px; margin:0px 0px 20px; border:none; }\r\np img, pre img, tt img, li img { margin-bottom:0px; } \r\n\r\nul { padding:0px; margin:0px 0px 20px 23px; list-style:square; }\r\nul li { padding:0px; margin:0px 0px 7px 0px; background:none; }\r\nul li ul { padding:5px 0px 0px; margin:0px 0px 7px 23px; }\r\nul li ol li { list-style:decimal; }\r\nol { padding:0px; margin:0px 0px 20px 0px; list-style:decimal; }\r\nol li { padding:0px; margin:0px 0px 7px 23px; list-style-type:decimal; }\r\nol li ol { padding:5px 0px 0px; margin:0px 0px 7px 0px; }\r\nol li ol li { list-style-type:lower-alpha; }\r\nol li ul { padding-top:7px; }\r\nol li ul li { list-style:square; }\r\n\r\npre, tt, code { font-size:12px; }\r\npre { margin:0px 0px 20px; }\r\npre.error { color:red; }\r\npre.codeinput { padding:10px; border:1px solid #d3d3d3; background:#f7f7f7; }\r\npre.codeoutput { padding:10px 11px; margin:0px 0px 20px; color:#4c4c4c; }\r\n\r\n@media print { pre.codeinput, pre.codeoutput { word-wrap:break-word; width:100%; } }\r\n\r\nspan.keyword { color:#0000FF }\r\nspan.comment { color:#228B22 }\r\nspan.string { color:#A020F0 }\r\nspan.untermstring { color:#B20000 }\r\nspan.syscmd { color:#B28C00 }\r\n\r\n.footer { width:auto; padding:10px 0px; margin:25px 0px 0px; border-top:1px dotted #878787; font-size:0.8em; line-height:140%; font-style:italic; color:#878787; text-align:left; float:none; }\r\n.footer p { margin:0px; }\r\n\r\n  <\/style><div class=\"content\"><!--introduction--><p>Recently on the MATLAB newsgroup, there was a <a>thread<\/a> asking how to split up a vector into pieces which were each monotonically increasing by the value 1.  The post got several answers which I did not read first.  Here's my thinking.<\/p><!--\/introduction--><h3>Contents<\/h3><div><ul><li><a href=\"#142b3afe-0cef-40a9-91a0-ae5ec7c55f60\">Sample Data<\/a><\/li><li><a href=\"#e011cd16-b34c-47f7-9b5e-003baa11e49f\">Problem and Solution<\/a><\/li><li><a href=\"#f926fc2c-95e4-487d-bce3-304b095dd23c\">File Exchange Function<\/a><\/li><li><a href=\"#798e7aa3-795c-4ae7-ab9c-29a41c8982b9\">Do You Need to Partition Data?<\/a><\/li><\/ul><\/div><h4>Sample Data<a name=\"142b3afe-0cef-40a9-91a0-ae5ec7c55f60\"><\/a><\/h4><p>Here's my sample data.<\/p><pre class=\"codeinput\">a=[2,3,4,7,9,12,13,14,15]\r\n<\/pre><pre class=\"codeoutput\">a =\r\n     2     3     4     7     9    12    13    14    15\r\n<\/pre><h4>Problem and Solution<a name=\"e011cd16-b34c-47f7-9b5e-003baa11e49f\"><\/a><\/h4><p>I want to break this into 4 pieces: the runs 2:4 and 12:15, plus the scalars 7 and 9.  Here's how I thought about the problem.<\/p><p>First I want to find the runs, which are elements that differ by 1.  So I calculate the differences, making sure I include the final value in the array (which is why I append a 0 below).<\/p><pre class=\"codeinput\">ad = [diff(a) == 1 0]\r\n<\/pre><pre class=\"codeoutput\">ad =\r\n     1     1     0     0     0     1     1     1     0\r\n<\/pre><p>Next, I figure out how many runs there are, by seeing how many 0 values are represented in the differences.  This tells me how many arrays I will split my original array into.<\/p><pre class=\"codeinput\">numcells = sum(ad==0)\r\nout = cell(1,numcells);\r\n<\/pre><pre class=\"codeoutput\">numcells =\r\n     4\r\n<\/pre><p>Next I find the ending indices of the chunks by looking where <tt>ad<\/tt> is 0. Then I start to create contents for each cell in the cell array <tt>out<\/tt>, starting with index 1 in the original array.<\/p><pre class=\"codeinput\">indends = find(ad == 0);\r\nind = 1;\r\n<span class=\"keyword\">for<\/span> k = 1:numcells\r\n   out{k} = a(ind:indends(k));\r\n   ind = indends(k)+1;\r\n<span class=\"keyword\">end<\/span>\r\n<\/pre><p>Here's what <tt>out<\/tt> looks like now.<\/p><pre class=\"codeinput\">out\r\nout{:}\r\n<\/pre><pre class=\"codeoutput\">out = \r\n    [1x3 double]    [7]    [9]    [1x4 double]\r\nans =\r\n     2     3     4\r\nans =\r\n     7\r\nans =\r\n     9\r\nans =\r\n    12    13    14    15\r\n<\/pre><h4>File Exchange Function<a name=\"f926fc2c-95e4-487d-bce3-304b095dd23c\"><\/a><\/h4><p>On the File Exchange, you can get a more general purpose function called <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/24255-splitvec\">SplitVec on SplitVec<\/a> by Bruno.  It does this and a whole lot more.<\/p><h4>Do You Need to Partition Data?<a name=\"798e7aa3-795c-4ae7-ab9c-29a41c8982b9\"><\/a><\/h4><p>If you need to partition data, I'd like to understand why, and how you do so.  Let me know <a href=\"https:\/\/blogs.mathworks.com\/loren\/?p=577#respond\">here<\/a>.<\/p><script language=\"JavaScript\"> <!-- \r\n    function grabCode_fb685f5238a64a7bbe47131f9b84fb79() {\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='fb685f5238a64a7bbe47131f9b84fb79 ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' fb685f5238a64a7bbe47131f9b84fb79';\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 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 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_fb685f5238a64a7bbe47131f9b84fb79()\"><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; R2012b<br><\/p><p class=\"footer\"><br>\r\n      Published with MATLAB&reg; R2012b<br><\/p><\/div><!--\r\nfb685f5238a64a7bbe47131f9b84fb79 ##### SOURCE BEGIN #####\r\n%% Partitioning a Vector\r\n% Recently on the MATLAB newsgroup, there was a\r\n% <http:\/\/view_thread\/324198#890764\r\n% thread> asking how to split up a vector into pieces which were each\r\n% monotonically increasing by the value 1.  The post got several answers\r\n% which I did not read first.  Here's my thinking.\r\n%% Sample Data\r\n% Here's my sample data.  \r\na=[2,3,4,7,9,12,13,14,15]\r\n%% Problem and Solution\r\n% I want to break this into 4 pieces: the runs 2:4\r\n% and 12:15, plus the scalars 7 and 9.  Here's how I thought about the\r\n% problem.\r\n%\r\n% First I want to find the runs, which are elements that differ by 1.  So I\r\n% calculate the differences, making sure I include the final value in the\r\n% array (which is why I append a 0 below).\r\nad = [diff(a) == 1 0]\r\n%%\r\n% Next, I figure out how many runs there are, by seeing how many 0 values\r\n% are represented in the differences.  This tells me how many arrays I will\r\n% split my original array into.\r\nnumcells = sum(ad==0)\r\nout = cell(1,numcells);\r\n%%\r\n% Next I find the ending indices of the chunks by looking where |ad| is 0.\r\n% Then I start to create contents for each cell in the cell array |out|,\r\n% starting with index 1 in the original array.\r\nindends = find(ad == 0);  \r\nind = 1;\r\nfor k = 1:numcells\r\n   out{k} = a(ind:indends(k));\r\n   ind = indends(k)+1;\r\nend\r\n%%\r\n% Here's what |out| looks like now.\r\nout\r\nout{:}\r\n%% File Exchange Function\r\n% On the File Exchange, you can get a more general purpose function called\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/24255-splitvec\r\n% SplitVec on SplitVec> by Bruno.  It does this and a whole lot more.\r\n%% Do You Need to Partition Data?\r\n% If you need to partition data, I'd like to understand why, and how you do\r\n% so.  Let me know <https:\/\/blogs.mathworks.com\/loren\/?p=577#respond here>.\r\n##### SOURCE END ##### fb685f5238a64a7bbe47131f9b84fb79\r\n-->","protected":false},"excerpt":{"rendered":"<!--introduction--><p>Recently on the MATLAB newsgroup, there was a <a>thread<\/a> asking how to split up a vector into pieces which were each monotonically increasing by the value 1.  The post got several answers which I did not read first.  Here's my thinking.... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/loren\/2012\/11\/14\/partitioning-a-vector\/\">read more >><\/a><\/p>","protected":false},"author":39,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[17,39,4],"tags":[],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/577"}],"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=577"}],"version-history":[{"count":5,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/577\/revisions"}],"predecessor-version":[{"id":2586,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/577\/revisions\/2586"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/media?parent=577"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/categories?post=577"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/tags?post=577"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}