MATLAB Answers

How can I insert a row in the middle of a matrix/vector?

335 views (last 30 days)
Alberto Benito
Alberto Benito on 3 Apr 2016
Commented: Dana Galili on 23 Jul 2021
My question is also about this subject, but appears a bit more complicated. I also need to insert new rows into a matrix (it's a vector column actually), but in different positions. Let's see: I have this column vector for instance:
[1;2;3;8;9;10;14;16;17]
what I need is to insert new elements in different places. As you can see, the values 4 5 6 7, 11 12 13 and 15 are missing in specific positions. In position 4 I need to insert 4 values (4 5 6 7), in position 7 I need to insert (11 12 13) and in position 8 I need to insert (15).
I used the function diff in order to calculate the difference in the "leaps" and I got somthing like this (11511421) I thought of using a loop for ----- for diff~=1 but after that I got stuck I didn't know to follow.
I would really thank to any who would give me an answer back. I'm waiting for any light!!
  1 Comment
Jan
Jan on 3 Apr 2016
Acoording to Imaga Analyst's answer: This sounds like a (too) complicated method to create the vector (1:17).' . Perhaps you simplified the problem too much.

Sign in to comment.

Answers (5)

Roger Stafford
Roger Stafford on 3 Apr 2016
Suppose you want to insert the column vector y in the column vector x starting at index ix.
x = [x(1:ix-1);y;x(ix,end)];
[Warning: If you plan to make other insertions afterwards on the same vector, its indexing will be different after the point ix.]

ConsistencyPLS
ConsistencyPLS on 31 Jan 2018
Edited: ConsistencyPLS on 1 Feb 2018
Say you have the matrix A given by
A = [1, 1;
2, 4;
3, 9;
4, 16;
5, 25;
6, 36;
7, 49];
and say that want to add the rows [1.5, 2.25] after row 1, [5.5, 30.25] after row 5 and [6.5, 42.25] after row 6. Lets put this information in the matrix B and vector ind,
B = [1.5, 2.25;
5.5, 30.25;
6.5, 42.25];
ind = [1, 5, 6];
Here is my solution for adding these rows in,
% Preallocate output
>> Anew = zeros(size(A,1)+size(B,1),size(A,2));
% Find indices for old data
>> addRows = ismember(1:size(A,1), ind)
addRows =
1×7 logical array
1 0 0 0 1 1 0
>> oldDataInd = (1:size(A,1)) + cumsum([0, addRows(1:end-1)])
oldDataInd =
1 3 4 5 6 8 10
% Add in old data
>> Anew(oldDataInd,:) = A(:,:)
Anew =
1 1
0 0
2 4
3 9
4 16
5 25
0 0
6 36
0 0
7 49
% Find indices for new data
>> newDataInd = (1:length(ind)) + ind
newDataInd =
2 7 9
% Add in new data
>> Anew(newDataInd,:) = B(:,:);
% Verify output
>> display(Anew)
Anew =
1.0000 1.0000
1.5000 2.2500
2.0000 4.0000
3.0000 9.0000
4.0000 16.0000
5.0000 25.0000
5.5000 30.2500
6.0000 36.0000
6.5000 42.2500
7.0000 49.0000
% Pass!
Hopefully this explanation is easy enough to follow and adapt.

Kuifeng
Kuifeng on 3 Apr 2016
Edited: Kuifeng on 4 Apr 2016
% Here is a walkaround for this problem
% 1. Define new vector Y based on existing vector x,
Y = ones(length(x) + No.Of.New.Values);
Y(:) = nan;
%2. Insert New.Values into respective cells of Y, for example
Y(4:7) = 4:7; %and others
% 3. after all new values inserted, find locations of NaN in Y;
locations = find(isnan(Y));
% 4. Replace the found locations of Y with existing x values;
Y(locations) = x;
  4 Comments

Sign in to comment.


Image Analyst
Image Analyst on 3 Apr 2016
If you simply need consecutive numbers, do this:
vec = (1:max(vec))'

Suresh Babu Annamraju
Suresh Babu Annamraju on 5 Aug 2020
A=[1;2;3;4;5]
add rows 2 and 5 to A to create B=[1;0;2;3;4;0;5]
A_temp=A;
index_rows=[2;5]
for i=1:size(index_rows,1)
mat1=A_temp(1:index_rows(i)-1);
mat2=A_temp(index_rows(i):end);
A_temp=[mat1;0;mat2];
end
B=A_temp;

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by