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?

3 Answers 3

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
Sign up to request clarification or add additional context in comments.

3 Comments

This works. But is it the best solution in terms of efficiency?
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?
I would add that [ e1 ; e2 ] translates to vertcat whereas [ e1 e2 ] or [ e1, e2 ] translate to horzcat. It is also useful to get familiar with cat for concatenating explicitly along any dimension.
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');

Comments

-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.

12 Comments

If A has n columns, you cannot concatenate it with a row/s having only one column. Please read the question again.
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.
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.`
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
@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.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.