Add a row to a matrix

andandandand picture andandandand · Nov 6, 2011 · Viewed 94.6k times · Source

I have a matrix A like

1 2 3 4 5
6 7 8 9 0

and I want to expand it with a row of ones to get

1 1 1 1 1
1 2 3 4 5
6 7 8 9 0 

I create the row of ones with

col_size = size(A, 2); 
ones_row = ones(1, col_size);

How can I add my ones_row to the matrix?

Answer

David Alber picture David Alber · Nov 6, 2011

Once you have A and ones_row you do:

[ones_row; A]

This returns the following.

1 1 1 1 1
1 2 3 4 5
6 7 8 9 0