20

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?

Dev-iL
  • 23,742
  • 7
  • 57
  • 99
andandandand
  • 21,946
  • 60
  • 170
  • 271

3 Answers3

42

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
David Alber
  • 17,624
  • 6
  • 65
  • 71
  • 1
    This works. But is it the best solution in terms of efficiency? – JustCurious Mar 15 '15 at 17:46
  • Hello, I have matrix $A,B$ of order $m\times n$, I want to write a matrix simply adding them row wise to get a $2m\times 2n$ matrix, what should I do? – Marso Oct 21 '16 at 06:10
  • I would add that `[ e1 ; e2 ]` translates to [`vertcat`](https://www.mathworks.com/help/matlab/ref/vertcat.html) whereas `[ e1 e2 ]` or `[ e1, e2 ]` translate to [`horzcat`](https://www.mathworks.com/help/matlab/ref/vertcat.html). It is also useful to get familiar with [`cat`](https://www.mathworks.com/help/matlab/ref/cat.html) for concatenating explicitly along any dimension. – Dev-iL Jun 28 '18 at 13:48
2

I would probably do it as suggested in the previous answer, however in some cases (when the matrix sizes get very big), a more memory friendly solution would be to preallocate a matrix of the correct size and use indexing to put the existing values in the correct place:

A = [ 1 2 3 4 5; 6 7 8 9 0 ];
B = ones(size(A) + [1,0]); % Create an array of ones that is one row longer
B(2:end,:) = A;            % Replace the elements of B with elements from A

The reason why I say this is more memory friendly is because when we create a row of ones we need to allocate memory for a vector, and then when we concatenate we need to allocate memory again for the result of the concatenation. When we use indexing, there's no need to allocate an intermediate vector. It isn't really important in this example, but can be quite significant for larger matrices or operations performed thousands of times.


There's also a useful function in the Image Processing Toolbox - padarray:

A = [ 1 2 3 4 5; 6 7 8 9 0 ];
B = padarray(A,[1 0],1,'pre');
Dev-iL
  • 23,742
  • 7
  • 57
  • 99
-1

I can give a solution that can worked for any matrix.

suppose your matrix is A, A is m*n

n = size(A,2)

out = [ones(1,n);A]

This solution works for any matrix.

PyMatFlow
  • 459
  • 4
  • 8
  • If A has `n` columns, you cannot concatenate it with a row/s having only one column. Please read the question again. – Sardar Usama Jun 28 '18 at 12:56
  • Read again my answer, it has not any relation to size of A. A can have n columns. Concentration is true, You can test in MATLAB. – PyMatFlow Jun 28 '18 at 13:01
  • 1
    For the sake of an example: `A = rand(5,6);` `n = size(A,2);` `out = [ones(n,1);A];` Error using vertcat. Dimensions of arrays being concatenated are not consistent.` – Sardar Usama Jun 28 '18 at 13:02
  • 1
    Now after the edit, your answer is exactly what is already posted by David Alber. This adds nothing new to the post. Please read the already posted answers before adding yours. Thanks – Sardar Usama Jun 28 '18 at 13:06
  • ones(n,1) changed to ones(1,n) – PyMatFlow Jun 28 '18 at 13:06
  • This adds nothing new and already posted as commented earlier – Sardar Usama Jun 28 '18 at 13:07
  • The answer of David Alber can not used for all matrix. It completes his answer. – PyMatFlow Jun 28 '18 at 13:08
  • Please read again. It is exactly the same and *can* be used for all matrices. Your answer just uses different variable names – Sardar Usama Jun 28 '18 at 13:09
  • I you read Urgent's comment, He has problem with this answer. – PyMatFlow Jun 28 '18 at 13:10
  • So you decided to provide exactly the same solution with different variable names. Okay – Sardar Usama Jun 28 '18 at 13:12
  • 1
    @PyMatFlow Urgent's comment is completely irrelevant and it should be a new question (if anything). The original question already contains `size(A, 2)` and `ones(1, ..);` - this is why Sardar said your answer adds nothing new. – Dev-iL Jun 28 '18 at 13:21
  • I think my solution can help MATLAB users. – PyMatFlow Jun 28 '18 at 13:26