MATLAB Answers

How to stick on/augmenting a vector into the matrix?

298 views (last 30 days)
Isti
Isti on 26 Apr 2012
Commented: Walter Roberson on 17 Apr 2017
If i have a matrix like this:
2 1 4 6 2
9 4 6 1 2
5 3 2 8 3
7 2 1 9 3
7 1 8 2 4
3 5 4 1 2
then i want to stick on a vektor contain of "1" in the beginning of that matrix. so it'll be like this:
1 2 1 4 6 2
1 9 4 6 1 2
1 5 3 2 8 3
1 7 2 1 9 3
1 7 1 8 2 4
1 3 5 4 1 2
what to do? thanks before :')

  2 Comments

Ashley Velasco
Ashley Velasco on 17 Apr 2017
I'm a student just learning how to use MATLAB... but I'm pretty sure you can use the horzcat function to do it easily. Matlab has documentation on it.
https://www.mathworks.com/help/matlab/ref/horzcat.html
Walter Roberson
Walter Roberson on 17 Apr 2017
The [] operator defaults to horzcat, so the solution I listed
[ones(size(TheMatrix,1),1), TheMatrix]
is really the same as
horzcat( ones(size(TheMatrix,1),1), TheMatrix)

Sign in to comment.

Accepted Answer

Walter Roberson
Walter Roberson on 26 Apr 2012
NewMatrix = [ones(size(TheMatrix,1),1), TheMatrix];

  2 Comments

Isti
Isti on 26 Apr 2012
i'm sorry but it's not suitable in my case. is there any?
thanks

Sign in to comment.

More Answers (2)

Isti
Isti on 26 Apr 2012
then the result is like this:
1 1 1 1 1 1 2 1 4 6 2
1 1 1 1 1 1 9 4 6 1 2
1 1 1 1 1 1 5 3 2 8 3
1 1 1 1 1 1 7 2 1 9 3
1 1 1 1 1 1 7 1 8 2 4
1 1 1 1 1 1 3 5 4 1 2
is there another solution?

  0 Comments

Sign in to comment.


ike
ike on 26 Apr 2012
yours= [ 2 1 4 6 2; 9 4 6 1 2; 5 3 2 8 3; 7 2 1 9 3; 7 1 8 2 4; 3 5 4 1 2]
add=ones(6,1); concat=[add yours]
try this....

  0 Comments

Sign in to comment.

Sign in to answer this question.


Translated by