{"id":2228,"date":"2008-05-16T08:03:33","date_gmt":"2008-05-16T13:03:33","guid":{"rendered":"https:\/\/blogs.mathworks.com\/pick\/2008\/05\/16\/inserting-rows\/"},"modified":"2016-05-11T10:07:45","modified_gmt":"2016-05-11T14:07:45","slug":"inserting-rows","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/pick\/2008\/05\/16\/inserting-rows\/","title":{"rendered":"Inserting Rows"},"content":{"rendered":"<div class=\"content\">\r\n\r\nJiro's pick this week is <a title=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/loadFile.do?objectId=9984&amp;objectType=file (link no longer works)\">INSERTROWS<\/a> by Jos van der Geest.\r\n\r\n&nbsp;\r\n\r\nThis function does exactly what the name says: it inserts rows into a matrix. What makes this significant is that this seemingly\r\ntrivial task is not straightforward to do in MATLAB. I can easily take rows out of a matrix, but putting rows in is not trivial.\r\n\r\nLet's say you have matrices <tt>mat1<\/tt> and <tt>mat2<\/tt>:\r\n<pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid #c8c8c8;\">mat1 = rand(5,3)\r\nmat2 = zeros(2,3)<\/pre>\r\n<pre style=\"font-style: oblique;\">mat1 =\r\n    0.5626    0.6264    0.9524\r\n    0.0009    0.4245    0.8164\r\n    0.7535    0.9275    0.2145\r\n    0.9500    0.4108    0.7153\r\n    0.6666    0.5620    0.7734\r\nmat2 =\r\n     0     0     0\r\n     0     0     0\r\n<\/pre>\r\nNow, suppose you want to insert <tt>mat2<\/tt> after the second row of <tt>mat2<\/tt>. You can use <tt>insertrows<\/tt> to do this:\r\n<pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid #c8c8c8;\">newMat = insertrows(mat1, mat2, 2)<\/pre>\r\n<pre style=\"font-style: oblique;\">newMat =\r\n    0.5626    0.6264    0.9524\r\n    0.0009    0.4245    0.8164\r\n         0         0         0\r\n         0         0         0\r\n    0.7535    0.9275    0.2145\r\n    0.9500    0.4108    0.7153\r\n    0.6666    0.5620    0.7734\r\n<\/pre>\r\nAs an added feature, this function also allows you to insert multiple rows into different locations of the original matrix.\r\nFor example, if you want to insert first row of <tt>mat2<\/tt> after row 2 and the second row of <tt>mat2<\/tt> after row 4:\r\n<pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid #c8c8c8;\">newMAT2 = insertrows(mat1, mat2, [2 4])<\/pre>\r\n<pre style=\"font-style: oblique;\">newMAT2 =\r\n    0.5626    0.6264    0.9524\r\n    0.0009    0.4245    0.8164\r\n         0         0         0\r\n    0.7535    0.9275    0.2145\r\n    0.9500    0.4108    0.7153\r\n         0         0         0\r\n    0.6666    0.5620    0.7734\r\n<\/pre>\r\nAlso <tt>mat2<\/tt> can be a single value, in which case a singleton expansion is performed:\r\n<pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid #c8c8c8;\">newMAT3 = insertrows(mat1, 100, [2 4])<\/pre>\r\n<pre style=\"font-style: oblique;\">newMAT3 =\r\n    0.5626    0.6264    0.9524\r\n    0.0009    0.4245    0.8164\r\n  100.0000  100.0000  100.0000\r\n    0.7535    0.9275    0.2145\r\n    0.9500    0.4108    0.7153\r\n  100.0000  100.0000  100.0000\r\n    0.6666    0.5620    0.7734\r\n<\/pre>\r\nFinally, I like how thorough the help and the examples are. Having good documentation greatly enhances the users' experiences,\r\nand I'm always impressed with the completeness of Jos's submissions. This function falls into one of my favorite categories\r\n- \"Simple, but Useful Functions\".\r\n\r\n<b>Comments<\/b>\r\n\r\nHere's a challenge for everyone. I've already asked people about their own \"simple functions\" in my previous <a href=\"https:\/\/blogs.mathworks.com\/pick\/2008\/04\/18\/renaming-variables\/\">blog post<\/a>, but try to think about the codes that you are writing everyday, and see if you can start creating your own modular functions,\r\nregardless of how trivial they may seem. You'll find that they will make your programs much more readable and manageable.\r\nThen consider posting them to the <a title=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/loadFile.do (link no longer works)\">File Exchange<\/a> and let us know about your experiences <a href=\"https:\/\/blogs.mathworks.com\/pick\/?p=2228#respond\">here<\/a>.\r\n\r\n<script>\/\/ <![CDATA[\r\nfunction grabCode_5043e58415f144dab33b0f22332407f9() {\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='5043e58415f144dab33b0f22332407f9 ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' 5043e58415f144dab33b0f22332407f9';\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 = 'Jiro Doke';\r\n        copyright = 'Copyright 2008 The MathWorks, Inc.';\r\n\r\n        w = window.open();\r\n        d = w.document;\r\n        d.write('\r\n\r\n\r\n\r\n\r\n\r\n<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>\r\n\r\n\r\n\r\n\r\n\r\n\r\n\\n');\r\n      \r\n      d.title = title + ' (MATLAB code)';\r\n      d.close();\r\n      }\r\n\/\/ ]]><\/script>\r\n<p style=\"text-align: right; font-size: xx-small; font-weight: lighter; font-style: italic; color: gray;\"><a><span style=\"font-size: x-small; font-style: italic;\">Get\r\nthe MATLAB code\r\n<noscript>(requires JavaScript)<\/noscript><\/span><\/a><\/p>\r\nPublished with MATLAB\u00ae 7.6\r\n\r\n<\/div>\r\n<!--\r\n5043e58415f144dab33b0f22332407f9 ##### SOURCE BEGIN #####\r\n%%\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/loadAuthor.do?objectId=1094142&objectType=author % Jiro>'s pick this week is <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/loadFile.do?objectId=9984&objectType=file % INSERTROWS> by <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/loadAuthor.do?objectType=author&objectId=1094450 % Jos van der Geest>.\r\n\r\n%%\r\n% This function does exactly what the name says: it inserts rows into a\r\n% matrix. What makes this significant is that this seemingly trivial task\r\n% is not straightforward to do in MATLAB. I can easily take rows out of a\r\n% matrix, but putting rows in is not trivial.\r\n%\r\n% Let's say you have matrices |mat1| and |mat2|:\r\n\r\nmat1 = rand(5,3)\r\nmat2 = zeros(2,3)\r\n\r\n%%\r\n% Now, suppose you want to insert |mat2| after the second row of |mat2|.\r\n% You can use |insertrows| to do this:\r\n\r\nnewMat = insertrows(mat1, mat2, 2)\r\n\r\n%%\r\n% As an added feature, this function also allows you to insert multiple\r\n% rows into different locations of the original matrix. For example, if you\r\n% want to insert first row of |mat2| after row 2 and the second row of\r\n% |mat2| after row 4:\r\n\r\nnewMAT2 = insertrows(mat1, mat2, [2 4])\r\n\r\n%%\r\n% Also |mat2| can be a single value, in which case a singleton expansion is\r\n% performed:\r\n\r\nnewMAT3 = insertrows(mat1, 100, [2 4])\r\n\r\n%%\r\n% Finally, I like how thorough the help and the examples are. Having good\r\n% documentation greatly enhances the users' experiences, and I'm always\r\n% impressed with the completeness of Jos's submissions. This function falls\r\n% into one of my favorite categories - \"Simple, but Useful Functions\".\r\n%\r\n% If you like this function, also take a look at\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/loadFile.do?objectId=10060&objectType=file % INTERLACE> by\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/loadAuthor.do?objectType=author&objectId=27836 % Duane>.\r\n%\r\n% *Comments*\r\n%\r\n% Here's a challenge for everyone. I've already asked people about their\r\n% own \"simple functions\" in my previous\r\n% <https:\/\/blogs.mathworks.com\/pick\/2008\/04\/18\/renaming-variables\/ blog % post>, but try to think about the codes that you are writing everyday,\r\n% and see if you can start creating your own modular functions, regardless\r\n% of how trivial they may seem. You'll find that they will make your\r\n% programs much more readable and manageable. Then consider posting them to\r\n% the <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/loadFile.do File % Exchange> and let us know about your experiences\r\n% <https:\/\/blogs.mathworks.com\/pick\/?p=2228#respond here>.\r\n##### SOURCE END ##### 5043e58415f144dab33b0f22332407f9\r\n-->","protected":false},"excerpt":{"rendered":"<p>\r\n\r\nJiro's pick this week is INSERTROWS by Jos van der Geest.\r\n\r\n&nbsp;\r\n\r\nThis function does exactly what the name says: it inserts rows into a matrix. What makes this significant is that this... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/pick\/2008\/05\/16\/inserting-rows\/\">read more >><\/a><\/p>","protected":false},"author":35,"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\/2228"}],"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\/35"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/comments?post=2228"}],"version-history":[{"count":2,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/2228\/revisions"}],"predecessor-version":[{"id":7129,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/2228\/revisions\/7129"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/media?parent=2228"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/categories?post=2228"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/tags?post=2228"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}