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

조회 수: 42 (최근 30일)
Alberto Benito
Alberto Benito 2016년 4월 3일
댓글: Dana Galili 2021년 7월 23일
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
Jan
Jan 2016년 4월 3일
편집: Jan 2016년 4월 3일
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.

댓글을 달려면 로그인하십시오.

답변 (5개)

Roger Stafford
Roger Stafford 2016년 4월 3일
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 2018년 1월 31일
편집: ConsistencyPLS 2018년 2월 1일
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.
  댓글 수: 3
Scott Prince
Scott Prince 2021년 1월 26일
This was a great find for me; thanks so much for posting this solution!
Dana Galili
Dana Galili 2021년 7월 23일
Thank you for this useful solution and detailed explaination!

댓글을 달려면 로그인하십시오.


Kuifeng
Kuifeng 2016년 4월 3일
편집: Kuifeng 2016년 4월 4일
% 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
Image Analyst
Image Analyst 2016년 4월 3일
Seem way more complicated than it needs to be. Also, not sure why Y is sometimes a 2D matrix and sometimes a 1D vector.
Kuifeng
Kuifeng 2016년 4월 4일
thank you for pointing this out.

댓글을 달려면 로그인하십시오.


Image Analyst
Image Analyst 2016년 4월 3일
If you simply need consecutive numbers, do this:
vec = (1:max(vec))'

Suresh Babu Annamraju
Suresh Babu Annamraju 2020년 8월 5일
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;

카테고리

Help CenterFile Exchange에서 Linear Prediction에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by