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

227 ビュー (過去 30 日間)
Giorgi
Giorgi 2015 年 1 月 28 日
コメント済み: Jayasudha 2024 年 3 月 5 日
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 件のコメント
Hikaru
Hikaru 2015 年 1 月 28 日
Is there a condition to be satisfied before performing this operation? Is it always going to be the 3rd position?
Giorgi
Giorgi 2015 年 1 月 28 日
Well its always going to be 3rd position

サインインしてコメントする。

採用された回答

Matz Johansson Bergström
Matz Johansson Bergström 2015 年 1 月 28 日
編集済み: Matz Johansson Bergström 2015 年 1 月 28 日
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,:)]
  6 件のコメント
Asif Newaz
Asif Newaz 2019 年 11 月 27 日
nice one mate
Jayasudha
Jayasudha 2024 年 3 月 5 日
Thak you!

サインインしてコメントする。

その他の回答 (2 件)

Stalin Samuel
Stalin Samuel 2015 年 1 月 28 日
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
  1 件のコメント
Riyadh Muttaleb
Riyadh Muttaleb 2017 年 3 月 14 日
Hi Stalin,
I would like to insert rows by using loop, your help would be so appreciated,
Riyadh

サインインしてコメントする。


Anil Kamat
Anil Kamat 2021 年 10 月 1 日
編集済み: Anil Kamat 2021 年 10 月 1 日
Just in case the insert position is variable.
emt_lpfc=[]; emt_rpfc = []; emt_lpmc =[]; emt_rpmc =[]; emt_sma=[];
A = rand(3,3)
emt_lpfc = 1;
emt_sma = 5;
emt_reg = [emt_lpfc emt_rpfc emt_lpmc emt_rpmc emt_sma];
temp = [];
if isempty(emt_reg) == 0
for i = 1:size(emt_reg,2)
%for filling rows
temp = A(emt_reg(i):end,:);
A(emt_reg(i),:)= zeros(size(A,1),1);
A (emt_reg(i)+1:end,:) = [];
A = [A ; temp];
%for filling columns
temp = [];
temp = A(:,emt_reg(i):end);
A(:,emt_reg(i))= zeros(1,size(A,1));
A (:,emt_reg(i)+1:end) = [];
A = [A temp];
end
end

カテゴリ

Help Center および File ExchangeSimulink Environment Customization についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by