# How can I insert row into matrix without deleting its values?

586 views (last 30 days)
Giorgi on 28 Jan 2015
Commented: Asif Newaz on 27 Nov 2019
Hello all! I want to insert row into the matrix, for example I have a=[1,2;4,9;11,12;3,6], and b=[0,0] ; now I want to insert b into a in the third position like this a=[1,2;4,9;0,0;11,12;3,6]. Any ideas?
##### 2 CommentsShowHide 1 older comment
Giorgi on 28 Jan 2015
Well its always going to be 3rd position

Matz Johansson Bergström on 28 Jan 2015
Edited: Matz Johansson Bergström on 28 Jan 2015
The only way I know of is to create a new matrix consisting of A and the row and then append the three matrices together, for instance
A = [1,1; 2,2; 3,3];
b = [6,7];
k = 0; %row position, can be 0,1,2 or 3 in this case
A = [A(1:k,:); b; A(k+1:end,:)]
Asif Newaz on 27 Nov 2019
nice one mate

Stalin Samuel on 28 Jan 2015
a=[1,2;4,9;11,12;3,6],
b=[0,0] ;
row_no=3 %%where wants to insert
a(1:row_no-1,:) = a(1:row_no-1,:)
tp =a(row_no:end,:)
a(row_no,:)=b
a(row_no+1:end+1,:) =tp
Riyadh Muttaleb on 14 Mar 2017
Hi Stalin,
I would like to insert rows by using loop, your help would be so appreciated,