MATLAB Answers

Append rows at the end of Matrix

4,681 ビュー (過去 30 日間)
Trushit 2014 年 1 月 27 日

Hi,
a = [1 2 3 ; 4 5 6; 7 8 9]; --> 3x3 matrix
I want to insert at the end number of raws with same elements such as [5 5 5] and make the matrix 10 x 3 i.e. I want to insert 7 more raws with [5 5 5]. Please explain.

0 件のコメント

サインイン to comment.

採用された回答

Azzi Abdelmalek 2014 年 1 月 27 日

a = [1 2 3 ; 4 5 6; 7 8 9];
b=[5 5 5]
c=[a;b]% add one row
c=[a;repmat(b,7,1)] %add 7rows

3 件のコメント

Trushit 2014 年 1 月 30 日
Thanks a lot. The answer also helps when we have different elements in stead of all same.
shubham shinde 2017 年 4 月 11 日
What if I have to insert the row somewhere between middle of the original matrix? Thank you in advance.
Hugh 2018 年 9 月 19 日
Yes, a long-time-after answer to a long-time-after question, but why not?
a = [1 2 3; 4 5 6; 7 8 9];
b = [5 5 5];
a = [a(1,1:3);b;a(2:3,1:3)]
Or for a bit more malleable solution (variable row to add to):
a = [1 2 3; 4 5 6; 7 8 9];
b = [5 5 5];
addrow = 2;
a = [a(1:addrow-1,1:3);b;a(addrow:3,1:3)]
Or a bit prettier, and even more malleable (if a has variable number of rows):
a = [1 2 3; 4 5 6; 7 8 9];
b = [5 5 5];
addrow = 2;
a = [a(1:addrow-1,:);b;a(addrow:end,:)]

サインイン to comment.

その他の回答 (2 件)

Amit 2014 年 1 月 27 日
A = [1 2 3;4 5 6;7 8 9];
A = [A; ones(7,3)*5];

1 件のコメント

Trushit 2014 年 1 月 30 日
Thank you. It's short and speedy.

サインイン to comment.

Michael Hawks 2019 年 5 月 2 日
Another method:
a = [1 2 3 ; 4 5 6; 7 8 9];
b=[5 5 5];
a( end+1, : ) = b;
or
a( :, end+1 ) = b';

0 件のコメント

サインイン to comment.

サインイン してこの質問に回答します。

Translated by