Effacer les filtres
Effacer les filtres

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

57 vues (au cours des 30 derniers jours)
Alberto Benito
Alberto Benito le 3 Avr 2016
Commenté : Dana Galili le 23 Juil 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 commentaire
Jan
Jan le 3 Avr 2016
Modifié(e) : Jan le 3 Avr 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.

Connectez-vous pour commenter.

Réponses (5)

Roger Stafford
Roger Stafford le 3 Avr 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 le 31 Jan 2018
Modifié(e) : ConsistencyPLS le 1 Fév 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.
  3 commentaires
Scott Prince
Scott Prince le 26 Jan 2021
This was a great find for me; thanks so much for posting this solution!
Dana Galili
Dana Galili le 23 Juil 2021
Thank you for this useful solution and detailed explaination!

Connectez-vous pour commenter.


Kuifeng
Kuifeng le 3 Avr 2016
Modifié(e) : Kuifeng le 4 Avr 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 commentaires
Image Analyst
Image Analyst le 3 Avr 2016
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 le 4 Avr 2016
thank you for pointing this out.

Connectez-vous pour commenter.


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

Suresh Babu Annamraju
Suresh Babu Annamraju le 5 Août 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;

Catégories

En savoir plus sur Matrix Indexing dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by