# Inserting Rows7

Posted by Jiro Doke,

Jiro's pick this week is INSERTROWS by Jos van der Geest.   This function does exactly what the name says: it inserts rows into a matrix. What makes this significant is that this seemingly trivial task is not straightforward to do in MATLAB. I can easily take rows out of a matrix, but putting rows in is not trivial. Let's say you have matrices mat1 and mat2:
mat1 = rand(5,3)
mat2 = zeros(2,3)
mat1 =
0.5626    0.6264    0.9524
0.0009    0.4245    0.8164
0.7535    0.9275    0.2145
0.9500    0.4108    0.7153
0.6666    0.5620    0.7734
mat2 =
0     0     0
0     0     0

Now, suppose you want to insert mat2 after the second row of mat2. You can use insertrows to do this:
newMat = insertrows(mat1, mat2, 2)
newMat =
0.5626    0.6264    0.9524
0.0009    0.4245    0.8164
0         0         0
0         0         0
0.7535    0.9275    0.2145
0.9500    0.4108    0.7153
0.6666    0.5620    0.7734

As an added feature, this function also allows you to insert multiple rows into different locations of the original matrix. For example, if you want to insert first row of mat2 after row 2 and the second row of mat2 after row 4:
newMAT2 = insertrows(mat1, mat2, [2 4])
newMAT2 =
0.5626    0.6264    0.9524
0.0009    0.4245    0.8164
0         0         0
0.7535    0.9275    0.2145
0.9500    0.4108    0.7153
0         0         0
0.6666    0.5620    0.7734

Also mat2 can be a single value, in which case a singleton expansion is performed:
newMAT3 = insertrows(mat1, 100, [2 4])
newMAT3 =
0.5626    0.6264    0.9524
0.0009    0.4245    0.8164
100.0000  100.0000  100.0000
0.7535    0.9275    0.2145
0.9500    0.4108    0.7153
100.0000  100.0000  100.0000
0.6666    0.5620    0.7734

Finally, I like how thorough the help and the examples are. Having good documentation greatly enhances the users' experiences, and I'm always impressed with the completeness of Jos's submissions. This function falls into one of my favorite categories - "Simple, but Useful Functions". Comments Here's a challenge for everyone. I've already asked people about their own "simple functions" in my previous blog post, but try to think about the codes that you are writing everyday, and see if you can start creating your own modular functions, regardless of how trivial they may seem. You'll find that they will make your programs much more readable and manageable. Then consider posting them to the File Exchange and let us know about your experiences here.

Get the MATLAB code

Published with MATLAB® 7.6

### Note

Brett replied on : 1 of 7

Very useful function, Jiro (and Jos!). I like that 0 works as an index; that makes it easy to interlace matrices A and B, starting _above_ the first row of A. So, for instance:
>> A = magic(3); B = rand(3); C = insertrows(A,B,0:2)
C =
0.0971 0.3171 0.4387
8.0000 1.0000 6.0000
0.8235 0.9502 0.3816
3.0000 5.0000 7.0000
0.6948 0.0344 0.7655
4.0000 9.0000 2.0000

Also, note that with a few transpose operators, INSERTROWS does double duty as INSERTCOLS:
>> D = insertrows(A’,B’,0:2)’
D =
0.0971 8.0000 0.3171 1.0000 0.4387 6.0000
0.8235 3.0000 0.9502 5.0000 0.3816 7.0000
0.6948 4.0000 0.0344 9.0000 0.7655 2.0000

I haven’t tried out Duane’s submission yet, but I’m going to keep Jos and Duane in mind when I need to flexibly intersperse matrices!

Tim Davis replied on : 2 of 7

Impressive performance on large sparse matrices. I tried it on a matrix of dimension 445,315 and 7,479,343 nonzeros (UFget(914) if you want to play with it; a large DNA model). It took about 8 seconds to insert 42 rows, as compared with about 1.5 seconds just to do C=A’.

That’s impressive because fiddling with the rows of a sparse matrix can easily take O(k*nnz(A)) time, if you do the wrong thing on k rows one at a time. This function gets it right.

Cool!

Rob S. replied on : 3 of 7

Jiro,

Your pick this week is similar to a small, modular function I use called “putBinA”. It plants a 2D matrix B into a larger 2D matrix A, with the upper-left corner of B at the given coordinates. It qualifies as a “simple function” you asked for, so I posted it to the file exchange (19989).

Coincidentally, putBinA was also bundled into a previous function that paginates multi-page tif files (scanned documents), which is also in the file exchange (17634).

Hope this is what you’re looking for.

jiro replied on : 4 of 7

Nice job, Rob. Thanks for posting the file.

I like that you included error checking in the function to make it robust to user errors.

Abiyu replied on : 5 of 7

Thank you so much!
It really was tremendous help!

Abiyu

saeedeh replied on : 6 of 7

Thank you very much for your information about inserting a row to matrix in matlab
I tried it but it seems it doesnot work in matlab 2009!
am I right?
will you please tell me how can I use this in my version?
thanks again

jiro replied on : 7 of 7

@saeedeh,

Did you download the function “insertrows” from the File Exchange? The link is in the beginning of this blog post.