{"id":2464,"date":"2009-09-04T12:33:51","date_gmt":"2009-09-04T12:33:51","guid":{"rendered":"https:\/\/blogs.mathworks.com\/pick\/2009\/09\/04\/concatenate-vectors-of-unequal-lengths\/"},"modified":"2009-09-04T12:37:34","modified_gmt":"2009-09-04T12:37:34","slug":"concatenate-vectors-of-unequal-lengths","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/pick\/2009\/09\/04\/concatenate-vectors-of-unequal-lengths\/","title":{"rendered":"Concatenate vectors of unequal lengths"},"content":{"rendered":"<div xmlns:mwsh=\"https:\/\/www.mathworks.com\/namespace\/mcode\/v1\/syntaxhighlight.dtd\" class=\"content\">\r\n   <introduction>\r\n      <p><a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/authors\/911\">Brett<\/a>'s Pick this week is <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/22909-padcat-varargin-\"><tt>padcat<\/tt><\/a>, by <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/authors\/10584\">Jos<\/a>--one of the File Exchange's most popular contributors.\r\n      <\/p>\r\n   <\/introduction>\r\n   <p>MATLAB has a few containers that are useful for storing heterogeneous data. In particular, one can store just about any combination\r\n      of variables or data types in <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2009b\/techdoc\/ref\/cell.html\">cell arrays<\/a>, <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2009b\/techdoc\/ref\/struct.html\">structures<\/a>, or (if you have the always useful <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2009b\/toolbox\/stats\/stats_product_page.html\">Statistics Toolbox<\/a>) <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2009b\/toolbox\/stats\/dataset.html\">dataset arrays<\/a>. Sometimes you want something lighter-weight, and easy to manipulate. If you just wanted to combine several row or column\r\n      vectors into a matrix, the function <tt>cat<\/tt> makes short work of it, if the vectors are the same length and orientation (all row vectors or all column vectors). If, on\r\n      the other hand, they had the same orientation but different lengths, <tt>cat<\/tt> wouldn't know what to do with them.\r\n   <\/p>\r\n   <p>With <tt>padcat<\/tt>, you could automatically combine them, padding shorter vectors with NaNs as necessary.\r\n   <\/p>\r\n   <p>Say, for example, that you had four row vectors of different length:<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">a = 1:5 ; b = 1:3 ; c = [] ; d = 1:4 ;<\/pre><p>and you wanted to create from them a single matrix. Try <tt>padcat<\/tt>:\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">M = padcat(a,b,c,d) <span style=\"color: #228B22\">% all are row vectors<\/span><\/pre><pre style=\"font-style:oblique\">M =\r\n     1     2     3     4     5\r\n     1     2     3   NaN   NaN\r\n   NaN   NaN   NaN   NaN   NaN\r\n     1     2     3     4   NaN\r\n<\/pre><p>As an added bonus, Jos's function can generate a second output that contains a binary mask showing <i>true<\/i> (or 1) where elements of M originated from an input vector, and <i>false<\/i> (or 0) where they were padded. This can be useful if any of the component vectors themselves contain NaNs. For instance:\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">a = [1:3]' ; b = [] ; c = [1;NaN] ;\r\n[M,tf] = padcat(a,b,c) <span style=\"color: #228B22\">% all are column vectors<\/span><\/pre><pre style=\"font-style:oblique\">M =\r\n     1   NaN     1\r\n     2   NaN   NaN\r\n     3   NaN   NaN\r\ntf =\r\n     1     0     1\r\n     1     0     1\r\n     1     0     0\r\n<\/pre><p>(Note that the second element in the third column of <i>tf<\/i> [i.e., row 2, column 3] is 1, indicating that the NaN in that position of M originated in vector <i>c<\/i>.)\r\n   <\/p>\r\n   <p>Just a nice utility function to have around. Thanks, Jos!<\/p>\r\n   <p><a href=\"https:\/\/blogs.mathworks.com\/pick\/?p=2464#respond\">Comments?<\/a><\/p><script language=\"JavaScript\">\r\n<!--\r\n\r\n    function grabCode_aff7cfc70d094f9ba2005072a548d460() {\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='aff7cfc70d094f9ba2005072a548d460 ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' aff7cfc70d094f9ba2005072a548d460';\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 = 'Brett Shoelson';\r\n        copyright = 'Copyright 2009 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_aff7cfc70d094f9ba2005072a548d460()\"><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.8<br><\/p>\r\n<\/div>\r\n<!--\r\naff7cfc70d094f9ba2005072a548d460 ##### SOURCE BEGIN #####\r\n%% Concatenate Vectors of Different Lengths\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/authors\/911 Brett>'s\r\n% Pick this week is <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/22909-padcat-varargin- |padcat|>,\r\n% by <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/authors\/10584 Jos>REPLACE_WITH_DASH_DASHone of the File Exchange's most popular contributors.\r\n\r\n%%\r\n% MATLAB has a few containers that are useful for storing heterogeneous\r\n% data. In particular, one can store just about any combination of variables or data types in\r\n% <https:\/\/www.mathworks.com\/help\/releases\/R2009b\/techdoc\/ref\/cell.html cell arrays>, <https:\/\/www.mathworks.com\/help\/releases\/R2009b\/techdoc\/ref\/struct.html structures>,\r\n% or (if you have the always useful <https:\/\/www.mathworks.com\/help\/releases\/R2009b\/toolbox\/stats\/stats_product_page.html Statistics Toolbox>)\r\n% <https:\/\/www.mathworks.com\/help\/releases\/R2009b\/toolbox\/stats\/dataset.html dataset arrays>.\r\n% Sometimes you want something lighter-weight, and easy to manipulate. If you just wanted to combine several row or column vectors into a matrix, \r\n% the function |cat| makes short work of it, if the vectors are the same length and orientation (all row vectors or all column vectors).\r\n% If, on the other hand, they had the same orientation but different lengths, |cat| wouldn't know what to do with them.\r\n\r\n%%\r\n% With |padcat|, you could automatically combine them, padding shorter vectors with NaNs as necessary.\r\n\r\n%%\r\n% Say, for example, that you had four row vectors of different length:\r\n\r\na = 1:5 ; b = 1:3 ; c = [] ; d = 1:4 ;\r\n\r\n%%\r\n% and you wanted to create from them a single matrix. Try |padcat|:\r\n\r\nM = padcat(a,b,c,d) % all are row vectors\r\n\r\n%%\r\n% As an added bonus, Jos's function can generate a second output that\r\n% contains a binary mask showing _true_ (or 1) where elements of M originated from an \r\n% input vector, and _false_ (or 0) where they were padded. This can be\r\n% useful if any of the component vectors themselves contain NaNs. For\r\n% instance:\r\n\r\na = [1:3]' ; b = [] ; c = [1;NaN] ;\r\n[M,tf] = padcat(a,b,c) % all are column vectors\r\n\r\n%%\r\n% (Note that the second element in the third column of _tf_ [i.e., row 2, column 3] is 1,\r\n% indicating that the NaN in that position of M originated in vector _c_.)\r\n\r\n%%\r\n% Just a nice utility function to have around. Thanks, Jos!\r\n%%\r\n% <https:\/\/blogs.mathworks.com\/pick\/?p=2464#respond Comments?> \r\n##### SOURCE END ##### aff7cfc70d094f9ba2005072a548d460\r\n-->","protected":false},"excerpt":{"rendered":"<p>\r\n   \r\n      Brett's Pick this week is padcat, by Jos--one of the File Exchange's most popular contributors.\r\n      \r\n   \r\n   MATLAB has a few containers that are useful for storing heterogeneous... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/pick\/2009\/09\/04\/concatenate-vectors-of-unequal-lengths\/\">read more >><\/a><\/p>","protected":false},"author":34,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[16],"tags":[],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/2464"}],"collection":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/users\/34"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/comments?post=2464"}],"version-history":[{"count":0,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/2464\/revisions"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/media?parent=2464"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/categories?post=2464"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/tags?post=2464"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}