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

45 ビュー (過去 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 Center および File ExchangeCharacters and Strings についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by